Add login for github

This commit is contained in:
wuxiaoshen
2017-04-20 18:42:34 +08:00
parent 4002ff942d
commit ad9d97202b
2 changed files with 82 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
### 1. 本地存储cookie
### 2. 程序运行
- post_param() : 先获取cookie
- bool_login() : 加载cookie 检验是否登入成功
+74
View File
@@ -0,0 +1,74 @@
#!/usr/bin/env python
# encoding: utf-8
"""
__author__: wuxiaoshen
__software__: PyCharm
__project__:scrapyone
__file__: github_login
__time__: 2017/4/16 23:20
"""
import requests
from lxml import etree
try:
import cookielib
except:
import http.cookiejar as cookielib
class GithubLogin(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()
self.session.cookies = cookielib.LWPCookieJar(filename='github_cookie')
def load_cookie(self):
try:
self.session.cookies.load(ignore_discard=True)
except:
print('cookie 不成功')
def get_param(self):
response = self.session.get(self.login_url, headers=self.headers)
selector = etree.HTML(response.text)
field_one = selector.xpath('//div/input[2]/@value')
print(field_one)
return field_one
pass
def post_param(self, email, password):
post_data = {
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': self.get_param()[0],
'login': email,
'password': password
}
response = self.session.post(self.post_url, data=post_data, headers=self.headers)
self.session.cookies.save()
pass
def bool_login(self):
self.load_cookie()
response = self.session.get(self.logined_url, headers=self.headers)
selector = etree.HTML(response.text)
flag = selector.xpath('//div[@class="column two-thirds"]/dl/dt/label/text()')
print(u'个人设置Profile包括: %s'%flag)
pass
if __name__ == "__main__":
Github = GithubLogin()
Github.post_param(email='******', password='******')
# Github.bool_login()