新浪微博需要登录才能爬取,这里使用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": (.*"https://m.weibo.cn/api/comments/show".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爬取新浪微博评论详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金钱帮资源网 Copyright www.kbjia.com
暂无“详解用python写网络爬虫-爬取新浪微博评论”评论...
更新日志
2024年10月11日
2024年10月11日
- 高胜美高胜云.2002-山地思情篇【皇星全音】【WAV+CUE】
- 逼迫中国选手弃权事件爆发后 万代官号发水豚君仰望天空
- 国外玩家恶搞奎爷去中国杀神:遇到孙悟空揍二郎神
- 育碧官宣《刺客信条幻景》上架Steam:本月正式发售
- 林俊杰.2015-和自己对话2CD【华纳】【WAV+CUE】
- 陈山河.1992-为你付出【喜玛拉雅】【WAV+CUE】
- 高胜美.1990-太阳雨【上格】【WAV+CUE】
- 苗苗(张云馨)《一往情深HQII》新京文唱片2021[WAV+CUE]
- 龚一《琴呼吸HQCD》WAV+CUE
- 伊能静.1989-悲伤的茱丽叶【飞鹰唱片】【WAV+CUE】
- 黑神话二郎神脸模再次COS自己:霸气侧漏 颜值超高!
- 《黑神话》萍萍演员再晒COS:国庆皮肤限时返场!
- 《暗黑4》资料片IGN 8分:新增内容令人相当满意!
- 《一人一首成名曲·怀旧篇 3CD》[WAV+CUE][1.5GB]
- 《张信哲 粤语精选集 从开始到现在 2CD》[WAV+CUE][780MB]