简单Python3爬虫程序(5)进阶:知乎网的登录与用户相关信息爬取
来源:互联网 发布:淘宝美工主要做什么 编辑:程序博客网 时间:2024/05/16 14:48
下面是在Python3上的代码,这次觉得麻烦就没有分成两个模块了,本来想爬的是每个用户的主页,但是正则没有写好,所以就爬取了所有与用户相关的信息:
import gzipimport refrom collections import dequeimport http.cookiejarimport urllib.requestimport urllib.parsequeue = deque()visited = set()def ungzip(data): try: print('正在解压.....') data = gzip.decompress(data) print('解压完毕!') except: print('未经压缩, 无需解压') return datadef getXSRF(data): cer = re.compile('name=\"_xsrf\" value=\"(.*)\"', flags = 0) strlist = cer.findall(data) return strlist[0]def getOpener(head): # deal with the Cookies 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 openerheader = { 'Connection': 'Keep-Alive', 'Accept': 'text/html, application/xhtml+xml, */*', '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.3; WOW64; Trident/7.0; rv:11.0) like Gecko', 'Accept-Encoding': 'gzip, deflate', 'Host': 'www.zhihu.com', 'DNT': '1'}url = 'http://www.zhihu.com/'opener = getOpener(header)op = opener.open(url)data = op.read()data = ungzip(data) _xsrf = getXSRF(data.decode())url += 'login/email'id = '账号'password = '密码'postDict = { '_xsrf':_xsrf, 'email': id, 'password': password, 'rememberme': 'y'}postData = urllib.parse.urlencode(postDict).encode()op = opener.open(url,data=postData)data = op.read()data = ungzip(data)saveFile(data)url = 'http://www.zhihu.com/'queue.append(url)cnt = 1while queue: url = queue.popleft() visited |= {url} print('已经抓取第: ' , cnt, '个链接',' 当前链接: ' + url) op = opener.open(url) if 'html' not in op.getheader('Content-Type'): continue try: data = ungzip(op.read()) saveFile(data) data = data.decode(encoding='UTF-8') print('save the :',cnt,' data') except: continue cnt += 1 linkre = re.compile('href=\"(.+?)\"') try: for x in linkre.findall(data): if 'http' in x and 'zhihu.com/people' in x and x not in visited: queue.append(x) print('把 ' + x +'加入队列') except: continue
其中saveFile函数请见简单Python3爬虫程序(4),欢迎留言!
我们要把要 POST 的数据弄成 opener.open() 支持的格式. 所以还要 urllib.parse 库里的 urlencode() 函数. 这个函数可以把字典、元组、集合类型的数据转换成 & 连接的 str,然后通过 encode() 来编码, 才能当作 opener.open() 或者 urlopen() 的 POST 数据参数来使用
1 1
- 简单Python3爬虫程序(5)进阶:知乎网的登录与用户相关信息爬取
- 简单Python3爬虫程序(3)进阶:登录、解压缩、cookies
- Python3爬虫小程序——爬取各类天气信息(2)
- Python3爬虫小程序——爬取各类天气信息(3)
- Python3爬虫小程序——爬取各类天气信息(4)
- python3.6.2实现的简单爬虫爬取百度百科
- Python3爬虫实战:爬取大众点评网某地区所有酒店相关信息
- Python3 爬虫学习(一):urllib库的使用及简单的爬取
- python3简单爬虫 (爬取各个网站上的图片)
- 【Python3.6爬虫学习记录】(一)爬取简单的静态网页图片
- Python3爬虫小程序——爬取各类天气信息
- python3.5简单爬虫爬取百度百科(参考imooc实战)
- 简单Python3爬虫程序(2)进阶:伪装浏览器、超时功能、保存数据
- python3爬虫攻略(4):简单的翻译程序
- Python3[爬虫实战] 爬虫之scrapy爬取爱上程序网存MongoDB(android模块)
- python3.5爬虫完成笔趣阁小说的爬取
- Python简单爬虫(爬取天气信息)
- Python 简单爬虫实现(爬取百度百科信息)
- js定时器
- javaCV在rcp中拍照
- Android 返回键的处理
- 有关scanf的输入格式不正确之说
- 加密 签名
- 简单Python3爬虫程序(5)进阶:知乎网的登录与用户相关信息爬取
- swift Defer语法
- Bzoj4516:[Sdoi2016]生成魔咒:哈希表+后缀自动机
- Fragment中onActivityResult不响应的解决办法
- 基于zookeeper的redis高可用
- 去掉导航栏 让标题栏与页面颜色一致
- Windows8 64安装Oracle 11g说明
- linux awk命令
- 屏蔽上架app的NSLog,降低资源消耗