[python爬虫]第二次的尝试:利用py爬虫来登陆知乎
2016-08-01 17:17
435 查看
这次按照的是 www.yiibai.com/python(小写)/python3-webbug-series4.html 的教程来写的,在原有代码的基础上稍微做了改动,体验了一把装逼的感觉,因为我测试用的知乎帐号是手机号注册的,所以代码上会有些出入
程序运行后的输出是:
Decompressing
It doesn't need decompress
Decompressing
It doesn't need decompress
{"r":0,
"msg": "\u767b\u5f55\u6210\u529f"
}
Login Success
其中\u767b\u5f55\u6210\u529f 是unicode编码,转换成中文就是“登录成功”
感觉还不错,接下来要在原理的层面上再深入理解爬虫,装高级一点的逼
#encoding utf-8 __author__ = 'qiao' import gzip import re import http.cookiejar import urllib.request import urllib.parse def get_opener(head): cj = http.cookiejar.CookieJar() pro = urllib.request.HTTPCookieProcessor(cj) opener = urllib.request.build_opener(pro) header = [] for key, value in head.items(): elem = (key,value) header.append(elem) opener.addheaders = header return opener def ungzip(data): try: print('Decompressing') data = gzip.decompress(data) print('Data decompression finished') except: print('It doesn\'t need decompress') return data def get_xsrf(data): cer = re.compile('name=\"_xsrf\" value=\"(.*)\"',flags = 0) strlist = cer.findall(data) return strlist[0] # test code header = { 'Connection': 'Keep-Alive', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Host': 'www.zhihu.com', 'DNT':'1' } url = 'http://www.zhihu.com/' opener = get_opener(header) op = opener.open(url) data = op.read() data = ungzip(data) _xsrf = get_xsrf(data.decode()) url += '/login/phone_num' id = '你的帐号' password = '你的密码' post_dict = { '_xsrf':_xsrf, 'phone_num':id, 'password': password, 'rememberme':'true', 'captcha':'' } post_data = urllib.parse.urlencode(post_dict).encode() op = opener.open(url,post_data) data = op.read() data = ungzip(data) print(data.decode()) print('Login Success')
程序运行后的输出是:
Decompressing
It doesn't need decompress
Decompressing
It doesn't need decompress
{"r":0,
"msg": "\u767b\u5f55\u6210\u529f"
}
Login Success
其中\u767b\u5f55\u6210\u529f 是unicode编码,转换成中文就是“登录成功”
感觉还不错,接下来要在原理的层面上再深入理解爬虫,装高级一点的逼
相关文章推荐
- Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
- Python2 爬虫(四) -- 模拟登陆(人人网和知乎)
- Python爬虫模拟登陆知乎
- python 爬虫利用webdriver 跳过登陆 进行小规模抓取信息
- Python 爬虫模拟登陆知乎
- Python爬虫——模拟登陆爬取知乎页面
- Python爬虫利用cookie实现模拟登陆实例详解
- 利用python requests库模拟登陆知乎
- Python 爬虫模拟登陆知乎
- python爬虫入门教程--利用requests构建知乎API(三)
- 第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别
- Py之Crawler:利用python尝试获取cn-proxy代理的IP地址——Jason niu
- Python爬虫之模拟登陆知乎
- Python爬虫基础——利用cookie实现模拟登陆
- Python 爬虫模拟登陆知乎
- Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
- [python和大数据-1]利用爬虫登录知乎进行BFS搜索抓取用户信息本地mysql分析【PART1】
- Python爬虫模拟登陆知乎
- 利用Python编写网络爬虫下载文章
- python人人语音爬虫(登陆尚未完成,需要使用先登录在查cookie中的t)