Python爬虫模拟真实登录案例系列之十二
2017-07-22 15:58
656 查看
一个模拟真实登录案例
import time import requests from bs4 import BeautifulSoup def captcha(data): with open("captcha.png", "wb") as f: f.write(data) # 返回输入的验证码字符串 return input("请输入验证码:") def login(): headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"} ssion = requests.session() # 获取登录页面的数据, URL地址不可变 html = ssion.get("https://www.zhihu.com/", headers=headers).content # 提取html文件,指定lxml文件解析器 soup = BeautifulSoup(html, "lxml") # 提取_xsrf值 _xsrf = soup.find('input', attrs={"name": "_xsrf"}).get("value") # 处理验证码url地址 captchaURL = "https://www.zhihu.com/captcha.gif?r=%d&type=login" % (time.time() * 1000) # 发送验证码图片的请求,返回图片数据 captcha_data = ssion.get(captchaURL, headers=headers).content # post登录表单数据 formdata = { "email": "123456789@qq.com", "password": "ABCDEFG", "captcha": captcha(captcha_data), "_xsrf": _xsrf } # 发送post登录请求,拿到cookie,URL地址不能变 ssion.post("https://www.zhihu.com/login/email", data=formdata, headers=headers) # 访问登录之后页面,并保存到本地 html = ssion.get("https://www.zhihu.com/settings/account#signin", headers=headers).content with open("myhtml.html", "w") as f: f.write(str(html.decode('utf-8'))) if __name__ == "__main__": login()
相关文章推荐
- Java爬虫系列之四模拟登录【模拟登录人人网】
- 【python爬虫03】使用Scrapy框架模拟登录知乎
- 【网络爬虫】【python】网络爬虫(三):模拟登录——伪装浏览器登录爬取过程
- python爬虫实战之模拟正方教务系统登录查询成绩
- Python爬虫实战——模拟登录教务系统
- python -- 拉勾网爬虫模拟登录
- Python爬虫模拟登录带验证码网站
- Python爬虫模拟登录带验证码网站
- python爬虫设置cookie模拟登录微博方法
- Python网络爬虫之模拟登录(以知乎为例)
- Python爬虫(四)——模拟登录imooc实战(利用cookie)
- python爬虫实践之模拟登录
- [置顶]定向爬虫 - Python模拟新浪微博登录
- python爬虫之模拟登录
- Python爬虫之模拟登录微信wechat
- Python爬虫模拟登录京东获取个人信息
- python爬虫(下)--模拟登录与Captcha识别
- Python爬虫与模拟登录
- Python爬虫模拟登录带验证码网站
- Python爬虫实战(5):模拟登录淘宝并获取所有订单