Python实操评论列表爬虫实战

import requests
import parsel
from pprint import pprint
import csv
import re
url = 'https://weibo.com/ajax/statuses/buildComments?is_reload=1&id=4940814809302669&is_show_bulletin=2&is_mix=0&count=10&uid=2142058927&fetch_level=0&locale=zh-CN'
headers = {
    'Cookie': '',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
}
res = requests.get(url=url, headers=headers)
f = open('评论.csv', mode='a', encoding='utf-8', newline='')

csv_write = csv.DictWriter(f, fieldnames=['用户', '地区', '评论内容', '评论时间'])
csv_write.writeheader()
for index in res.json()['data']:
    # pprint(index)
    text = index['text']
    content = ''.join(re.findall('[\u4e00-\u9fa5]+', text))
    # pprint(index)
    dit = {
        '用户': index['user']['screen_name'],
        '地区': index['source'].replace('来自', ''),
        '评论内容': content,
        '评论时间': index['created_at']
    }
    csv_write.writerow(dit)

你可能感兴趣的:(Python,python,爬虫,开发语言)