详解用python写网络爬虫-爬取新浪微博评论
作者:Joliph 时间:2021-09-30 22:23:11
新浪微博需要登录才能爬取,这里使用m.weibo.cn这个移动端网站即可实现简化操作,用这个访问可以直接得到的微博id。
分析新浪微博的评论获取方式得知,其采用动态加载。所以使用json模块解析json代码
单独编写了字符优化函数,解决微博评论中的嘈杂干扰字符
本函数是用python写网络爬虫的终极目的,所以采用函数化方式编写,方便后期优化和添加各种功能
# -*- coding:gbk -*-
import re
import requests
import json
from lxml import html
#测试微博4054483400791767
comments=[]
def get_page(weibo_id):
url='https://m.weibo.cn/status/{}'.format(weibo_id)
html=requests.get(url).text
regcount=r'"comments_count": (.*?),'
comments_count=re.findall(regcount,html)[-1]
comments_count_number=int(comments_count)
page=int(comments_count_number/10)
return page-1
def opt_comment(comment):
tree=html.fromstring(comment)
strcom=tree.xpath('string(.)')
reg1=r'回复@.*?:'
reg2=r'回覆@.*?:'
reg3=r'//@.*'
newstr=''
comment1=re.subn(reg1,newstr,strcom)[0]
comment2=re.subn(reg2,newstr,comment1)[0]
comment3=re.subn(reg3,newstr,comment2)[0]
return comment3
def get_responses(id,page):
url="https://m.weibo.cn/api/comments/show?id={}&page={}".format(id,page)
response=requests.get(url)
return response
def get_weibo_comments(response):
json_response=json.loads(response.text)
for i in range(0,len(json_response['data'])):
comment=opt_comment(json_response['data'][i]['text'])
comments.append(comment)
weibo_id=input("输入微博id,自动返回前5页评论:")
weibo_id=int(weibo_id)
print('\n')
page=get_page(weibo_id)
for page in range(1,page+1):
response=get_responses(weibo_id,page)
get_weibo_comments(response)
for com in comments:
print(com)
print(len(comments))
以上所述是小编给大家介绍的python爬取新浪微博评论详解整合网站的支持!
来源:https://blog.csdn.net/Joliph/article/details/77334354
标签:python,爬取,新浪微博评论
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中函数的参数定义和可变参数用法实例分析
2023-10-04 00:26:30
Select下拉列表控件美化
2008-11-12 12:55:00
Python中基本的日期时间处理的学习教程
2023-08-25 08:16:19
![](https://img.aspxhome.com/file/2023/6/81636_0s.jpg)
Python匹配中文的正则表达式
2022-03-21 18:05:50
网页图片按钮的生成与美化
2008-12-12 13:03:00
使用Python的Twisted框架编写非阻塞程序的代码示例
2021-01-22 16:20:51
SaaS中的用户体验设计
2009-05-20 12:28:00
![](https://img.aspxhome.com/file/UploadPic/20095/20/p2009520103356-74s.jpg)
Django使用channels + websocket打造在线聊天室
2022-01-05 11:16:26
![](https://img.aspxhome.com/file/2023/9/76729_0s.gif)
Python 实现判断图片格式并转换,将转换的图像存到生成的文件夹中
2023-07-19 04:13:23
基于JS实现html中placeholder属性提示文字效果示例
2023-09-07 22:50:58
![](https://img.aspxhome.com/file/2023/0/56130_0s.gif)
django请求返回不同的类型图片json,xml,html的实例
2021-05-17 10:03:23
对Python3+gdal 读取tiff格式数据的实例讲解
2023-03-10 10:21:53
python爬虫selenium和phantomJs使用方法解析
2023-02-06 19:23:38
![](https://img.aspxhome.com/file/2023/6/69496_0s.png)
python实现过滤敏感词
2021-02-26 04:23:17
![](https://img.aspxhome.com/file/2023/4/93614_0s.png)
用Python编写分析Python程序性能的工具的教程
2022-02-13 01:57:03
![](https://img.aspxhome.com/file/2023/3/63553_0s.jpg)
解决“引入同一个JS文件在非IE6中正常,但IE6报错”的问题
2009-04-03 11:42:00
apache集成php7.3.5的详细步骤
2023-08-20 16:31:05
![](https://img.aspxhome.com/file/2023/9/55479_0s.png)
ASP中如何判断一个字符是不是汉字
2008-05-04 12:47:00
利用Python抢回在蚂蚁森林逝去的能量(实现代码)
2022-07-01 15:15:39
![](https://img.aspxhome.com/file/2023/6/90646_0s.jpg)
如何使用Python多线程测试并发漏洞
2021-11-18 08:25:52
![](https://img.aspxhome.com/file/2023/9/66299_0s.jpg)