目的:动态获取cookie
第一:分析登陆过程
1、打开开发者工具,查看各自请求
2、可以看到name为session的请求【方式post,传入的data】
3、查看name为login的请求,源码中获得token,作为上一个请求中的data的一部分
第二:登陆以后,解析页面信息流数据
查看信息流请求的url,自行构建对应的url,解析
1、这里有个技巧 /div[@id=111]//text() 获取div下面所以的text()
2、正则的re.sub(' +',' ',str)实现多个空格替换成单独一个空格
def dd(self):
para={
'page': self.page,
'utf8':'✓'
}
xml=self.xmlurl+ urlencode(para)
res=self.session.get(xml,headers=self.heads)
if res.status_code==200:
sele=etree.HTML(res.text)
dynamics=sele.xpath('//div[@class="d-flex flex-justify-between flex-items-baseline"]')
print(dynamics)
for i in dynamics:
str=''.join(i.xpath('./div//text()')).replace('\n','')#一个div的所有[//]text()全部解析出来,然后构成一个类别,并把换行符替换
str=re.sub(' +',' ',str)#多个空白字符,换成一个
print(str)
第三:整体封装成一个类方便调用
这里重点关注类的__init__()初始设置的属性,3个不同的url,当然和Session()会话的调用
from lxml import etree
import requests
from urllib.parse import urlencode
import re
class gitlogain():
def __init__(self,page):
self.heads={
'Referer': 'https://github.com',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 YaBrowser/18.6.1.770 Yowser/2.5 Safari/537.36',
'Host':'github.com'
}
self.logainurl='https://github.com/login'
self.posturl='https://github.com/session'
self.session=requests.Session()
self.page=page
self.xmlurl='https://github.com/dashboard-feed?'
def token(self):
ruler=re.compile(r'name="authenticity_token" value="(.*)"./')
auth=self.session.get(self.logainurl,headers=self.heads)
z=auth.text
authenticity_token=ruler.findall(z)
print(authenticity_token[0])
return authenticity_token[0]
def login(self,email,password):
post_data={
'commit': 'Sign in',
'utf8':'✓',
'authenticity_token':self.token(),
'login': email,
'password': password
}
ss=self.session.post(self.posturl,headers=self.heads,data=post_data)
if ss.status_code==200:
self.dd()
def dd(self):
para={
'page': self.page,
'utf8':'✓'
}
xml=self.xmlurl+ urlencode(para)
res=self.session.get(xml,headers=self.heads)
if res.status_code==200:
sele=etree.HTML(res.text)
dynamics=sele.xpath('//div[@class="d-flex flex-justify-between flex-items-baseline"]')
print(dynamics)
for i in dynamics:
str=''.join(i.xpath('./div//text()')).replace('\n','')#一个div的所有[//]text()全部解析出来,然后构成一个类别,并把换行符替换
str=re.sub(' +',' ',str)#多个空白字符,换成一个
print(str)