python模拟登陆知乎
---恢复内容开始---
在完成前面的阶段的任务之后,我们现在已经能够尝试着去模拟登录一些网站了。在这里我们模拟登录一下知乎做一下实验。笔者在这里总共用了三天多的时间,下面给大家分享一下笔者是怎么一步一步的模拟登录成功的。也希望大家能够吸取我的教训。 初步的模拟登录 下面这段代码是笔者最初写的,我们慢慢来看 import requestsfrom bs4 import BeautifulSoup as bs
ssesion = requests.session()
headers = {
'Connection': 'keep-alive',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, sdch',
'Host': 'www.zhihu.com',
}
login_data = {'username': '', # 替换为账号 'password': '', # 替换为密码 'remember_me': 'true',
'Referer': 'https://www.baidu.com/',
}
response = bs(requests.get('http://www.zhihu.com/#signin').content, 'html.parser') xsrf = response.find('input',attrs={'name':'_xsrf'})['value'] login_data['_xsrf'] =xsrf responed = ssesion.post('http://www.zhihu.com/login/email',headers=headers,data=login_data) print(responed) 在最初的写模拟登录知乎的时候,笔者也是通过抓包,发现了,cookie中有一个_xsrf的属性,类似于token的作用。而这个东西的存在,就让我们在模拟登录的时候,必须将这个属性作为参数一起加在请求中发送出去,那么怎么获得这个东西呢?似乎又是一个问题。 我想到的方法,就是随便访问一个页面,然后再页面元素中去定位到_xsrf这个字段,然后抓取下来,添加到data里,在请求的时候一起发出去就可以了。 然后为什么会去用ssesion去请求,因为在知乎上,它的xsrf是一直在变化的,我们每一次请求,它都在变。所以如果我们用requests去请求的话,就无法登录成功。 那么上面这段代码基本已经符合我们的要求了。我们运行看一下结果
response = bs(requests.get('http://www.zhihu.com/#signin').content, 'html.parser')
print(response) xsrf = response.find('input',attrs={'name':'_xsrf'})['value'] print(xsrf) 在这里,分开进行打印,以便查看到底是走到哪一步出的错。 运行这一段代码得到结果如下显示: Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/Practice/Login_zhihu.py", line 6, in
500 Server Error
An internal server error occured.frombs4importBeautifulSoupasbs
headers = {
'Connection':'keep-alive',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language':'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
'Accept-Encoding':'gzip, deflate, sdch',
'Host':'www.zhihu.com',
}
login_data = {'username':'',# 替换为账号
'password':'',# 替换为密码
'remember_me':'true',
'Referer':'https://www.baidu.com/',
}
response = bs(requests.get('http://www.zhihu.com/#signin',headers=headers).content,'html.parser')
xsrf = response.find('input',attrs={'name':'_xsrf'})['value'] print(xsrf) 好的,在运行看看: 899ce2556d7e705ca9bbf2b818a48d40
from bs4 import BeautifulSoup as bs
ssesion = requests.session()
headers = {
'Connection': 'keep-alive',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, sdch',
'Host': 'www.zhihu.com',
}
login_data = {'username': '', # 替换为账号
'password': '', # 替换为密码
'remember_me': 'true',
'Referer': 'https://www.baidu.com/',
}
response = bs(requests.get('http://www.zhihu.com/#signin',headers=headers).content, 'html.parser')
xsrf = response.find('input',attrs={'name':'_xsrf'})['value']
login_data['_xsrf'] =xsrf
responed = ssesion.post('http://www.zhihu.com/login/email',headers=headers,data=login_data) print(responed) 运行这段代码得到的结果是
---恢复内容结束---