import requests
from lxml import etree class Login(object):
def __init__(self):
self.headers = {
'Referer': 'https://github.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Host': 'github.com'
}
self.login_url = 'https://github.com/login'
self.post_url = 'https://github.com/session'
self.logined_url = 'https://github.com/settings/profile'
self.session = requests.Session() #方便请求头cookie的处理 def token(self):
response = self.session.get(self.login_url, headers=self.headers)
selector = etree.HTML(response.text)
token = selector.xpath('//div//input[2]/@value')
return token def login(self, email, password):
post_data = {
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': self.token()[0],
'login': email,
'password': password
}
response = self.session.post(self.post_url, data=post_data, headers=self.headers)
if response.status_code == 200:
self.dynamics(response.text) response = self.session.get(self.logined_url, headers=self.headers)
if response.status_code == 200:
self.profile(response.text) def dynamics(self, html):
selector = etree.HTML(html)
dynamics = selector.xpath('//div[contains(@class, "news")]//div[contains(@class, "alert")]')
for item in dynamics:
dynamic = ' '.join(item.xpath('.//div[@class="title"]//text()')).strip()
print(dynamic) def profile(self, html):
selector = etree.HTML(html)
name = selector.xpath('//input[@id="user_profile_name"]/@value')[0]
email = selector.xpath('//select[@id="user_profile_email"]/option[@value!=""]/text()')
print(name, email) if __name__ == "__main__":
login = Login()
login.login(email='cqc@cuiqingcai.com', password='password')

  

模拟登陆github的更多相关文章

  1. Python爬虫教程:requests模拟登陆github

    1. Cookie 介绍 HTTP 协议是无状态的.因此,若不借助其他手段,远程的服务器就无法知道以前和客户端做了哪些通信.Cookie 就是「其他手段」之一. Cookie 一个典型的应用场景,就是 ...

  2. python模拟登陆Github示例

    首先进入github登录页:https://github.com/login 输入账号密码,打开开发者工具,在Network页勾选上Preserve Log(显示持续日志),点击登录,查看Sessio ...

  3. 模拟登陆Github示例

    首先进入github登录页:https://github.com/login 输入账号密码,打开开发者工具,在Network页勾选上Preserve Log(显示持续日志),点击登录,查看Sessio ...

  4. Python爬虫学习笔记之模拟登陆并爬去GitHub

    (1)环境准备: 请确保已经安装了requests和lxml库 (2)分析登陆过程:     首先要分析登陆的过程,需要探究后台的登陆请求是怎样发送的,登陆之后又有怎样的处理过程.      如果已经 ...

  5. 模拟登陆并爬取Github

    因为崔前辈给出的代码运行有误,略作修改和简化了. 书上例题,不做介绍. import requests from lxml import etree class Login(object): def ...

  6. pytho简单爬虫_模拟登陆西电流量查询_实现一键查询自己的校园网流量

    闲来无事,由于校园内网络是限流量的,查询流量很是频繁,于是萌生了写一个本地脚本进行一键查询自己的剩余流量. 整个部分可以分为三个过程进行: 对登陆时http协议进行分析 利用python进行相关的模拟 ...

  7. 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)

    urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...

  8. 第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别

    第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别 第一步.首先下载,大神者也的倒立文字验证码识别程序 下载地址:https://gith ...

  9. python-爬虫之requests模块介绍(登陆github)

    介绍 使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) 注意 requests库发送请求将网页内容下载下来以后 ...

随机推荐

  1. 【Linux基础】Linux命令date 日期时间

    1.显示到纳秒 date +%F.%H:%M:%S.%N --:38.740127086 date +%Y-%m-%d.%H:%M:%S.%N2019-04-25.00:28:24.060756673 ...

  2. Java中数组和集合的foreach操作编译后究竟是啥

    今天和同事在关于foreach编译后是for循环还是迭代器有了不同意见,特做了个Demo,了解一下. 是啥自己来看吧! public class Demo { public static void m ...

  3. 类Math

    概述 java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数.类似这样的工具 类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单. 常用方法 ​ ...

  4. Nginx 在线新增模块

    系统:Centos7.5 Nginx版本:1.12.2 今天给项目添加ssl证书时,发现nginx 竟然不支持ssl,经过查看,询问相关人员发现nginx编译的时候没有任何模块(历史原因).哎.... ...

  5. [蓝桥杯]2015蓝桥省赛B组题目及详解

    /*——————————————————————————————————————————————————————————— [结果填空题]T1 题目:奖券数目 有些人很迷信数字,比如带“4”的数字,认 ...

  6. MySQL中的用户与授权

    grant all on *.* to root@'192.168.20.49'; grant select on *.* to root@192.168.20.49 ; -- dba 可以查询 My ...

  7. Springboot读取Jar文件中的resource

    如题,碰到了问题. 事情是这样的. 一个导入模板, 因为比较少, 所以就直接放在后台的resources中了.调试的时候是下载没有问题的. 等到发布后,下载就出问题了. 参照: ***.jar!\BO ...

  8. 「Algospot」量化QUANTIZE

    一道不难的DP题,主要是为了总结这类最优化题的思路:同时还学到了一个新操作 传送门:$>here<$ 题意 给出一个长度为$N$的序列,要求最多使用s个数字进行量化(有损压缩),即代替原数 ...

  9. 使用apidoc生成项目文档

    [1]npm install apidoc -g 全局安装apidoc [2]apidoc -v 查看是否安装成功 [3]apidoc.json apidoc的项目级配置文件,它必须位于整个工程目录顶 ...

  10. linux的/etc/profile、~/.profile、~/.bashrc、~./bash_profile这几个配置文件

    在添加环境变量的时候,我们会去修改配置文件 如果留意过,网上博文,有些在/etc/profile文件中配置的,有些是在~./bash_profile文件中配置的,等等 那么,/etc/profile. ...