国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 詳解用python寫網絡爬蟲-爬取新浪微博評論

詳解用python寫網絡爬蟲-爬取新浪微博評論

2021-06-25 00:41Joliph Python

這篇文章主要介紹了python爬取新浪微博評論,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

新浪微博需要登錄才能爬取,這里使用m.weibo.cn這個移動端網站即可實現簡化操作,用這個訪問可以直接得到的微博id。

分析新浪微博的評論獲取方式得知,其采用動態加載。所以使用json模塊解析json代碼

單獨編寫了字符優化函數,解決微博評論中的嘈雜干擾字符

本函數是用python寫網絡爬蟲的終極目的,所以采用函數化方式編寫,方便后期優化和添加各種功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- 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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲一区在线视频 | 日韩亚洲一区二区 | 亚洲一区二区在线 | 欧美日韩激情 | 91精品国产乱码久久久久久 | 日韩成人在线一区二区 | 亚洲精品在线中文字幕 | 欧美日韩中文在线观看 | 成人免费视频视频在线观看 免费 | 在线免费观看a视频 | 激情欧美一区二区免费视频 | 亚洲一区观看 | 久久久久久久av | 亚洲免费看片 | 免费看黄在线观看 | www久 | 国产99精品视频 | 网站色| 91久久夜色精品国产网站 | 欧美,日韩,国产精品免费观看 | 一区二区在线 | 亚洲精品短视频 | 欧美一区二区三区精品 | 国产成人精品综合 | 日韩久久久久久 | 天天操夜夜操av | 精品国产91乱码一区二区三区 | 亚洲精品国产综合区久久久久久久 | 欧美成人久久久免费播放 | 日韩久久精品 | 中国大陆高清aⅴ毛片 | 久久久久久久久久久免费av | 好吊色欧美一区二区三区四区 | 蜜桃传媒一区二区 | 成人欧美一区二区三区色青冈 | 亚洲人成网站b2k3cm | 亚洲国产成人91精品 | 性大毛片视频 | 色婷婷久久一区二区三区麻豆 | 中文天堂在线观看视频 | 久久久午夜爽爽一区二区三区三州 |