import requests
import re
import parsel
import csv
url = 'https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6'
headers = {
'User-Agent': '',
'Cookie': ''
}
responst = requests.get(url=url, headers=headers)
selector = parsel.Selector(responst.text)
trs = selector.css('#pl_top_realtimehot tbody tr')
num = 1
# csv 配置项
f = open('热搜.csv', mode='a', encoding='utf-8', newline='')
csv_write = csv.DictWriter(f, fieldnames=['排序', '标题', '热度'])
for tr in trs:
title = tr.css('.td-02 a::text').get()
hot = tr.css('.td-03 .icon-txt::text').get()
num += 1
dit = {
'排序': num,
'标题': title,
'热度': hot
}
# csv保存
csv_write.writerow(dit)
print(dit)