Skip to content
This repository has been archived by the owner on Jul 13, 2019. It is now read-only.

Add login for github #72

Merged
merged 1 commit into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions 018 Github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

### 1. 本地存储cookie


### 2. 程序运行

- post_param() : 先获取cookie
- bool_login() : 加载cookie 检验是否登入成功
74 changes: 74 additions & 0 deletions 018 Github/github_login.py
Original file line number Diff line number Diff line change
@@ -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()