用python做有趣的事儿——模拟登陆知乎
来源:互联网 发布:淘宝八十字评论 编辑:程序博客网 时间:2024/06/02 19:32
用Python做网络数据采集,是非常方便的。特别是使用第三方库requests。
window下安装方法 pip install requests
处理网页超文本信息,使用第三方库BeautifulSoup,也是很方便。
window下安装方法 pip install beautifulsoup4
通常会用到的是requests.get()和requests.post()。其他的,当你需要的时候去官方文档是最好的捷径。
我们处理的还是HTML,了解一些HTML是必须的,起码得知道标签,属性,属性值等等,只有这样我们才能在一堆信息里提取出我们想要得信息。
最近在看《Python网络数据采集》,打算拿知乎当作练手的网站(心疼知乎-。-)。知乎毕竟是企业级的网站,在模拟登录的时候遇到了很多挫折,对,就是挫折,前几天因为一直登录不上还放弃过(呸,丢人的家伙)。今天不经意的在书中看到隐藏字段,恍然大悟。原来知乎的表单里有隐藏字段啊。
通过BeautifulSoup过滤出隐藏字段,然后post,但是还是不能登录成功,很恼火。逼不得已上网看了别人的源码,shit,竟然有验证码,我在知乎的网页端没看见啊,所以我把验证码的这快程序加上,结果,,,,登录成功。好吧,知乎你赢了。
# -*- coding: utf-8 -*-'''Info- auther : Cherish_x- qq : 344799672- data : 2017.9.8'''import reimport timeimport requestsfrom io import BytesIOfrom PIL import Imagefrom bs4 import BeautifulSouploadpost_url = 'https://www.zhihu.com/login/phone_num'load_url = 'https://www.zhihu.com/#signin'agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36'headers = { 'User-Agent': agent, 'Referer': 'https://www.zhihu.com/', 'Host': 'www.zhihu.com'}FormData = { '_xsrf': '', 'password': 'xu344799672', 'phone_num': '17853530696'}#———————————————————————————————————————————————————————————# 模拟登陆知乎def anaLoad(load_url, loadpost_url, headers, FormData): #————————————————————————————————————————————————————————— # 保持会话信息 session = requests.session() #————————————————————————————————————————————————————————— # 获取动态参数_xsrf r = session.get(load_url, headers=headers) _xsrf = r.cookies['_xsrf'] FormData['_xsrf'] = _xsrf #————————————————————————————————————————————————————————— # 获取验证码,并以照片的形式打开,手动输入 randomtime = str(int(time.time() * 1000)) captchaurl = 'https://www.zhihu.com/captcha.gif?r=' + \ randomtime + "&type=login" captcharesponse = session.get(url=captchaurl, headers=headers) tempIm = BytesIO(captcharesponse.content) # 将数据流放入tempIm以字节的形式 im = Image.open(tempIm) # 转换为图片的形式 im.show() # 打开 captch = input('Please input coding:') #————————————————————————————————————————————————————————— # 登录 FormData['captcha'] =captch posthtml = session.post(loadpost_url, data=FormData, headers=headers) #print(posthtml.cookies) msg = eval(posthtml.text)# 转为字典格式 print(msg['msg']) return session #返回会话信息,以备下次使用#———————————————————————————————————————————————————————————# 跳转知乎内链def throughUrl(session, headers): urlList = [] r = session.get('https://www.zhihu.com/#signin', headers=headers) bs0bj = BeautifulSoup(r.text, 'html.parser') Text = bs0bj.findAll('meta', {'itemprop': 'url'}) for i in Text: i = i.attrs['content'] if i not in urlList and 'www.zhihu.com' in i:# 去重、去外链(知乎专栏,不能跳转,待解决) urlList.append(i) accessUrl(session, i, headers, urlList)#———————————————————————————————————————————————————————————# 访问链接def accessUrl(session, url, headers, urlList): r = session.get(url, headers=headers) bs0bj = BeautifulSoup(r.text, 'html.parser') #text = bs0bj.findAll('span', {'itemprop': 'text'}) text = bs0bj.findAll('meta', {'itemprop': 'name'}) print(text) print('——————————————————————————————————————————————————') html = bs0bj.findAll('meta', {'itemprop': 'url'})# 寻找链接所在 for i in html: i = i.attrs['content'] if i not in urlList and 'www.zhihu.com' in i:# 去重、去外链(知乎专栏,不能跳转,待解决) urlList.append(i) time.sleep(3) accessUrl(session, i, headers, urlList)if __name__ == '__main__': session = anaLoad(load_url, loadpost_url, headers, FormData) throughUrl(session, headers)
目前实现了模拟登陆知乎,遍历采集的知乎内链(去重检查),我会把这个项目一直更新写下去的,除非真的没有功能可以拓展了。也以此深入了解学习网络数据采集。值得注意的是,一定要有延时,应为知乎的防爬意识很严肃,在调试程序的时候,我就经常被知乎封ip,此时需要用网页登录一次输入验证码即可。 好吧,知乎就你了!
- 用python做有趣的事儿——模拟登陆知乎
- 用Python做有趣的事儿——模拟登陆学校教务系统
- python---模拟登陆知乎
- python 模拟知乎登陆
- 用Python做有趣的事儿——打包程序为.exe
- 使用python完成知乎模拟登陆
- Python爬虫模拟登陆知乎
- python模拟登陆知乎,得到cookie
- Python 爬虫模拟登陆知乎
- Python爬虫之模拟登陆知乎
- Python爬虫模拟登陆知乎
- 模拟登陆知乎
- python网络爬虫入门(二)———模拟登陆知乎
- Python 模拟知乎登陆,保存登陆cookie
- python模拟登陆知乎和CSDN【urllib,re,requests】
- python爬虫 scrapy框架 知乎zhihu 模拟登陆
- 利用python requests库模拟登陆知乎
- python模拟登陆知乎(手工识别验证码)
- 在一个字符串中,找出最长回文子串
- Kinect sdk 2.0 + Opencv 获取深度图像并保存
- java线程死锁
- ImportError: No module named arcpy
- windows窗体程序的创建
- 用python做有趣的事儿——模拟登陆知乎
- Java多线程--让主线程等待子线程执行完毕
- PAT乙级1062. 最简分数(20)
- Mysql only_full_group_by以及其他关于sql_mode原因报错详细解决方案
- (1668): 割点和割边
- Java实现主线程等待子线程
- makefile 指定文件的生成目录
- 1035. Password (20)
- Python高级特性(切片 迭代 列表生成式 生成器 迭代器)学习笔记