最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解用python寫(xiě)網(wǎng)絡(luò)爬蟲(chóng)-爬取新浪微博評(píng)論

 更新時(shí)間:2019年05月10日 16:33:59   作者:Joliph  
這篇文章主要介紹了python爬取新浪微博評(píng)論,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

新浪微博需要登錄才能爬取,這里使用m.weibo.cn這個(gè)移動(dòng)端網(wǎng)站即可實(shí)現(xiàn)簡(jiǎn)化操作,用這個(gè)訪(fǎng)問(wèn)可以直接得到的微博id。

分析新浪微博的評(píng)論獲取方式得知,其采用動(dòng)態(tài)加載。所以使用json模塊解析json代碼

單獨(dú)編寫(xiě)了字符優(yōu)化函數(shù),解決微博評(píng)論中的嘈雜干擾字符

本函數(shù)是用python寫(xiě)網(wǎng)絡(luò)爬蟲(chóng)的終極目的,所以采用函數(shù)化方式編寫(xiě),方便后期優(yōu)化和添加各種功能

# -*- coding:gbk -*-
import re
import requests
import json
from lxml import html
#測(cè)試微博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'回復(fù)@.*?:'
  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,自動(dòng)返回前5頁(yè)評(píng)論:")
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爬取新浪微博評(píng)論詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

任丘市| 嘉义县| 诸城市| 红原县| 定西市| 闻喜县| 大埔区| 唐海县| 湟源县| 兴业县| 安溪县| 耿马| 扶余县| 铁岭县| 大名县| 永德县| 新宁县| 井陉县| 射洪县| 中山市| 通化市| 且末县| 阿克苏市| 西昌市| 遂昌县| 阿图什市| 中山市| 土默特左旗| 依安县| 黑水县| 张家港市| 阿勒泰市| 遂川县| 峨眉山市| 星子县| 吴堡县| 红桥区| 醴陵市| 南投县| 承德县| 仪征市|