详解用python写网络爬虫-爬取新浪微博评论


Posted in Python onMay 10, 2019

新浪微博需要登录才能爬取,这里使用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爬取新浪微博评论详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
python实现数通设备tftp备份配置文件示例
Apr 02 Python
python中去空格函数的用法
Aug 21 Python
解读Python中degrees()方法的使用
May 18 Python
Python实现获取域名所用服务器的真实IP
Oct 25 Python
pandas数据分组和聚合操作方法
Apr 11 Python
对python for 文件指定行读写操作详解
Dec 29 Python
python实现简单加密解密机制
Mar 19 Python
FFrpc python客户端lib使用解析
Aug 24 Python
Python爬取知乎图片代码实现解析
Sep 17 Python
Python实现弹球小游戏
Aug 01 Python
Python tkinter界面实现历史天气查询的示例代码
Aug 23 Python
使用python绘制分组对比柱状图
Apr 21 Python
python的turtle库使用详解
May 10 #Python
详解Python sys.argv使用方法
May 10 #Python
Python3.5 + sklearn利用SVM自动识别字母验证码方法示例
May 10 #Python
python lxml中etree的简单应用
May 10 #Python
基于Python的PIL库学习详解
May 10 #Python
python导入坐标点的具体操作
May 10 #Python
python简单验证码识别的实现方法
May 10 #Python
You might like
PHP+AJAX实现无刷新注册(带用户名实时检测)
2006/12/02 PHP
php Xdebug 调试扩展的安装与使用.
2010/03/13 PHP
yii框架数据库关联查询操作示例
2019/10/14 PHP
使用PHP+Redis实现延迟任务,实现自动取消订单功能
2019/11/21 PHP
jQuery 各种浏览器下获得日期区别
2008/12/22 Javascript
关于flash遮盖div浮动层的解决方法
2010/07/17 Javascript
基于json的jquery地区联动效果代码
2011/07/06 Javascript
做web开发 先学JavaScript
2014/12/12 Javascript
使用FlexiGrid实现Extjs表格效果方法分享
2014/12/16 Javascript
js如何实现淡入淡出效果
2020/11/18 Javascript
ionic实现滑动的三种方式
2016/08/27 Javascript
jQuery插件fullPage.js实现全屏滚动效果
2016/12/02 Javascript
微信小程序 特效菜单抽屉效果实例代码
2017/01/11 Javascript
JavaScript中双符号的运算详解
2017/03/12 Javascript
实例详解JavaScript中setTimeout函数的执行顺序
2017/07/12 Javascript
JS 中LocalStorage和SessionStorage的使用
2017/08/17 Javascript
基于vue实现分页效果
2017/11/06 Javascript
通过nodejs 服务器读取HTML文件渲染到页面的方法
2018/05/17 NodeJs
js技巧之十几行的代码实现vue.watch代码
2018/06/09 Javascript
Vue2.0学习系列之项目上线的方法步骤(图文)
2018/09/25 Javascript
NProgress显示顶部进度条效果及使用详解
2019/09/21 Javascript
微信小程序监听用户登录事件的实现方法
2019/11/11 Javascript
JS实现烟花爆炸效果
2020/03/10 Javascript
Python字符串处理函数简明总结
2015/04/13 Python
Python使用win32com模块实现数据库表结构自动生成word表格的方法
2018/07/17 Python
python中import与from方法总结(推荐)
2019/03/21 Python
如何通过python画loss曲线的方法
2019/06/26 Python
详解Python中pyautogui库的最全使用方法
2020/04/01 Python
加拿大最大的五金、家居装修和园艺产品商店:RONA
2017/01/27 全球购物
eBay德国站:eBay.de
2017/09/14 全球购物
大学同学十年聚会感言
2014/02/21 职场文书
给校长的一封检讨书
2014/09/20 职场文书
个人四风问题原因分析及整改措施
2014/09/28 职场文书
自我推荐信格式模板
2015/03/24 职场文书
离职证明范本
2015/06/12 职场文书
Ubuntu Server 安装Tomcat并配置systemctl
2022/04/28 Servers