91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

給定微博id和爬取評論數量,爬取對應微博的評論,便于下一步的分詞和詞頻統計

資源截圖

代碼片段和文件信息

#?這里是完整代碼!?。。。?!
#?完整爬取微博評論程序,只需要修改微博id即可
import?requests
import?json
import?re
#爬取微博評論寫入weibo_comment.txt
def?get_comment(weibo_id?url?headers?number):
????count?=?0
????fp?=?open(“weibo_comment_“+str(weibo_id)+“.txt“?“a“?encoding=“utf8“)
????#判斷爬取數目是否足夠
????while?count????????#判斷是否是第一組,第一組不加max_id
????????if?count?==?0:
????????????print(‘是第一組‘)
????????????try:
????????????????url?=?url?+?weibo_id?+?‘&mid=‘?+?weibo_id?+‘&max_id_type=0‘
????????????????web_data?=?requests.get(url?headers?=?headers)
????????????????js_con?=?web_data.json()
????????????????#獲取連接下一頁評論的max_id
????????????????max_id?=?js_con[‘data‘][‘max_id‘]
????????????????print(max_id)
????????????????comments_list?=?js_con[‘data‘][‘data‘]
????????????????for?commment_item?in?comments_list:
????????????????????comment?=?commment_item[“text“]
????????????????????#刪除表情符號
????????????????????label_filter?=?re.compile(r‘]*>‘?re.S)
????????????????????comment?=?re.sub(label_filter?‘‘?comment)
????????????????????fp.write(comment)
????????????????????count?+=?1
????????????????????print(“已獲取“+str(count)+“條評論?!埃?br/>????????????except?Exception?as?e:
????????????????print(str(count)?+?“遇到異?!埃?br/>????????????????continue
???????

評論

共有 條評論