Python爬取新浪微博評論資料,寫入csv檔案中

2021-09-07 13:13:46 字數 1221 閱讀 2035

操作步驟如下:

2.開啟m.weibo.cn

3.查詢自己感興趣的話題,獲取對應的資料介面鏈結

4.獲取cookies和headers

#

-*- coding: utf-8 -*-

import

requests

import

csvimport

osbase_url = '

'cookies =

headers =

path = os.getcwd()+"

/weibo.csv

"csvfile = open(path, '

a+', encoding='

utf-8

',newline=''

)writer =csv.writer(csvfile)

writer.writerow((

'username

','source

','comment'))

for i in range(0,83):

try:

url = base_url.format(page=i)

resp = requests.get(url, headers=headers, cookies=cookies)

jsondata =resp.json()

data = jsondata.get('

data')

for d in

data:

created_at = d.get("

created_at")

source = d.get("

source")

username = d.get("

user

").get("

screen_name")

comment = d.get("

text")

print

((username,source,comment))

writer.writerow((username, source, comment))

except

:

print('

*'*1000)

pass

csvfile.close()

至於爬出來的資料有非中文的資料,要提取中文請參考:篩選出一段文字中的中文

未完待續。。。。

爬取新浪微博

學到的東西。1 習慣用logger,而不是用print self.logger.debug 開始解析 format response.url 2 習慣用正規表示式 這是在pipeline清理資料時用到的 s 5分鐘前 if re.match d 分鐘前 s minute re.match d s g...

用python寫網路爬蟲 爬取新浪微博評論

首先微博的站點有四個,pc 端weibo.com weibo.cn以及移動端m.weibo.com 無法在電腦上瀏覽 在網上大致瀏覽了一下,普遍都認為移動端爬取比較容易,故選擇移動端進行爬取。登陸m.weibo.cn之後,找到指定微博,例如如下微博 detail 4493649780161355 找...

自動獲取cookie,爬取新浪微博熱門評論

目錄 一 前言 二 網盤 selenium僅僅用於獲取cookie,實際爬取將直接使用requests請求,以保證爬取效率 話不多說,也不複雜,直接上 了,關鍵的地方有注釋 import requests import selenium from selenium import webdriver ...