python爬虫爬取网页信息教程(python爬虫爬取微博评论案例详解)
python爬虫爬取网页信息教程
python爬虫爬取微博评论案例详解前几天,杨超越编程大赛火了,大家都在报名参加,而我也是其中的一员。
在我们的项目中,我负责的是数据爬取这块,我主要是把对于杨超越 的每一条评论的相关信息。
数据格式:{"name":评论人姓名,"comment_time":评论时间,"comment_info":评论内容,"comment_url":评论人的主页}
以上就是我们需要的信息。
爬虫前的分析:
以上是杨超越的微博主页,这是我们首先需要获取到的内容。
因为我们需要等到这个主页内这些微博详情页 的链接,但是我们向下刷新,会发现微博的主页信息是ajax动态加载出来的,
这张图片就是我们向下刷新获取到 的新的链接,这个就是我们需要获取到的信息页面信息。
接下来 就是获取详情页面的信息,详情页中含有评论的相关信息,通过向下刷新,我们也会发现,相关的评论信息也是通过ajax加载出来的 ,
ok,以上就是我们针对整个流程大致的一个分析过程。
具体操作流程:
我们首相将主页获取完成以后,我们就会发现,其中 的内容带有相关的反爬措施,获取到的源码中的信息含有很多的转义符“\”,并且其中的相关“<”和“>”是通过html的语言直接编写的,这样会导致我们的页面解析出现一定的问题,我们可以用replace方法直接将这些转义符全部去掉,然后我们就可以对这个页面进行正则处理,同时我也尝试过用其他的解析方法,但是其中遇到了很多 的问题,所以我就不过多的介绍了。
当我们获取到了每一篇微博的链接以后,还需要获取一个很关键的值 id ,这个值有什么用呢,其主要的作用就是在评论页面的ajax页面的拼接地址上需要使用到。接下来就是需要寻找出我们找到的这两个ajax的url有什么特点或者是规律:
当我们从这些ajax中找到规律以后,不难发现,这个爬虫差不多大功告成了。
下面我就展示一下我的代码:
注意:请在headers中添加自己的cookie
|
# -*- coding: utf-8 -*- # created : 2018/8/26 18:33 # author :guoli import requests import json import time from lxml import etree import html import re from bs4 import beautifulsoup class weibospider: def __init__( self ): # 获取首页的相关信息: self .start_url = 'https://weibo.com/u/5644764907?page=1&is_all=1' self .headers = { "accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/.jpg" alt="python爬虫爬取网页信息教程(python爬虫爬取微博评论案例详解)" border="0" /> "accept-encoding" : "gzip, deflate, br" , "accept-language" : "zh-cn,zh;q=0.9,en;q=0.8" , "cache-control" : "max-age=0" , "cookie" : 使用自己本机的cookie, "referer" : "https://www.weibo.com/u/5644764907?topnav=1&wvr=6&topsug=1" , "upgrade-insecure-requests" : "1" , "user-agent" : "mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/72.0.3626.96 safari/537.36" , } self .proxy = { 'http' : 'http://180.125.70.78:9999' , 'http' : 'http://117.90.4.230:9999' , 'http' : 'http://111.77.196.229:9999' , 'http' : 'http://111.177.183.57:9999' , 'http' : 'http://123.55.98.146:9999' , } def parse_home_url( self , url): # 处理解析首页面的详细信息(不包括两个通过ajax获取到的页面) res = requests.get(url, headers = self .headers) response = res.content.decode().replace( "\\", " ") # every_url = re.compile('target="_blank" href="(/\d+/\w+\?from=\w+&wvr=6&mod=weibotime)" rel="external nofollow" ', re.s).findall(response) every_id = re. compile ( 'name=(\d+)' , re.s).findall(response) # 获取次级页面需要的id home_url = [] for id in every_id: base_url = 'https://weibo.com/aj/v6/comment/big?ajwvr=6&id={}&from=singleweibo' url = base_url. format ( id ) home_url.append(url) return home_url def parse_comment_info( self , url): # 爬取直接发表评论的人的相关信息(name,info,time,info_url) res = requests.get(url, headers = self .headers) response = res.json() count = response[ 'data' ][ 'count' ] html = etree.html(response[ 'data' ][ 'html' ]) name = html.xpath( "//li[@class='list_li s_line1 clearfix']/li[@class='wb_face w_fl']/a/img/@alt" ) # 评论人的姓名 info = html.xpath( "//li[@node-type='replywrap']/li[@class='wb_text']/text()" ) # 评论信息 info = " ".join(info).replace(" ", " ").split(" \n") info.pop( 0 ) comment_time = html.xpath( "//li[@class='wb_from s_txt2']/text()" ) # 评论时间 name_url = html.xpath( "//li[@class='wb_face w_fl']/a/@href" ) # 评论人的url name_url = [ "https:" + i for i in name_url] comment_info_list = [] for i in range ( len (name)): item = {} item[ "name" ] = name[i] # 存储评论人的网名 item[ "comment_info" ] = info[i] # 存储评论的信息 item[ "comment_time" ] = comment_time[i] # 存储评论时间 item[ "comment_url" ] = name_url[i] # 存储评论人的相关主页 comment_info_list.append(item) return count, comment_info_list def write_file( self , path_name, content_list): for content in content_list: with open (path_name, "a" , encoding = "utf-8" ) as f: f.write(json.dumps(content, ensure_ascii = false)) f.write( "\n" ) def run( self ): start_url = 'https://weibo.com/u/5644764907?page={}&is_all=1' start_ajax_url1 = 'https://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100406&is_all=1&page={0}&pagebar=0&pl_name=pl_official_myprofilefeed__20&id=1004065644764907&script_uri=/u/5644764907&pre_page={0}' start_ajax_url2 = 'https://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100406&is_all=1&page={0}&pagebar=1&pl_name=pl_official_myprofilefeed__20&id=1004065644764907&script_uri=/u/5644764907&pre_page={0}' for i in range ( 12 ): # 微博共有12页 home_url = self .parse_home_url(start_url. format (i + 1 )) # 获取每一页的微博 ajax_url1 = self .parse_home_url(start_ajax_url1. format (i + 1 )) # ajax加载页面的微博 ajax_url2 = self .parse_home_url(start_ajax_url2. format (i + 1 )) # ajax第二页加载页面的微博 all_url = home_url + ajax_url1 + ajax_url2 for j in range ( len (all_url)): print (all_url[j]) path_name = "第{}条微博相关评论.txt" . format (i * 45 + j + 1 ) all_count, comment_info_list = self .parse_comment_info(all_url[j]) self .write_file(path_name, comment_info_list) for num in range ( 1 , 10000 ): if num * 15 < int (all_count) + 15 : comment_url = all_url[j] + "&page={}" . format (num + 1 ) print (comment_url) try : count, comment_info_list = self .parse_comment_info(comment_url) self .write_file(path_name, comment_info_list) except exception as e: print ( "error:" , e) time.sleep( 60 ) count, comment_info_list = self .parse_comment_info(comment_url) self .write_file(path_name, comment_info_list) del count time.sleep( 0.2 ) print ( "第{}微博信息获取完成!" . format (i * 45 + j + 1 )) if __name__ = = '__main__' : weibo = weibospider() weibo.run() |
以上所述是小编给大家介绍的python爬虫爬取微博评论详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对开心学习网网站的支持!
原文链接:https://blog.csdn.net/qq_41733098/article/details/88539402
- Python HTML解析器BeautifulSoup用法实例详解【爬虫解析器】(Python HTML解析器BeautifulSoup用法实例详解爬虫解析器)
- python opencv 标记目标(使用Python的OpenCV模块识别滑动验证码的缺口推荐)
- python操作pandas(详解Python学习之安装pandas)
- python怎么测试api接口(python接口自动化测试之接口数据依赖的实现方法)
- python 文件操作(Python File文件 方法整理)
- python图片aes加密(python实现AES加密和解密)
- python循环创建字典(Python字典的基本用法实例分析创建、增加、获取、修改、删除)
- python使用aes加密解密(python实现AES加密与解密)
- python串口怎么使用(使用Python3+PyQT5+Pyserial 实现简单的串口工具方法)
- python线程池有几种(对python 多线程中的守护线程与join的用法详解)
- python3有哪些内置模块(Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析)
- python弹球游戏编写过程(python实现坦克大战游戏 附详细注释)
- 如何查看python是否安装selenium(selenium+python截图不成功的解决方法)
- python常用列表函数和方法(Python enumerate函数功能与用法示例)
- knn算法详细步骤(Python实现KNNK-近邻算法的示例代码)
- python 操作html(Python HTML解析模块HTMLParser用法分析爬虫工具)
- 三兄妹感情再遇波折,人设接连崩塌 《以家人之名》剧情猜不透(三兄妹感情再遇波折)
- 《小敏家》金波想要复婚 这只是他圈套的第1步,更可恶的在后面(小敏家金波想要复婚)
- 小敏家 剧情离谱一锅乱炖,但他们俩绝对是这部剧的一大 亮点(剧情离谱一锅乱炖)
- 《倚天屠龙记》再遭翻拍,关晓彤主演赵敏,蒋劲夫演张无忌,你怎么看(倚天屠龙记再遭翻拍)
- 吴启华与曾舜晞两代张无忌同框,戏里经典的他却没活出原著的潇洒(吴启华与曾舜晞两代张无忌同框)
- 经常发这三种 朋友圈 的人,要迅速屏蔽(经常发这三种朋友圈)
热门推荐
- 新手如何搭建ftp服务器(ftp服务器想要在外网中访问怎么搭建)
- kmp算法怎么用c描述(详解小白之KMP算法及python实现)
- css实现隐藏菜单(利用CSS实现几款不错的菜单栏实例代码)
- vue封装组件技巧(浅谈vue中所有的封装方式总结)
- docker-compose绑定端口失效(docker-compose创建网桥,添加子网,删除网卡的实现)
- mysql并发查询优化(详解MySQL 联合查询优化机制)
- php符串使用教程学习(PHP实现支持CURL字符串证书传输的方法)
- linq中join用法
- 阿里云服务器防御(阿里云服务器攻击超过云盾防御阀值应该怎么办?)
- opencv调用摄像头图像识别(Python+OpenCV采集本地摄像头的视频)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9