爬取哔哩哔哩月排行榜,并输出csv格式文件。
import requests
import lxml.html
import csv
source = requests.get('https://www.bilibili.com/ranking/all/0/0/30').content.decode('utf-8')
selector = lxml.html.fromstring(source)
items = selector.xpath('//ul[@class="rank-list"]/li')
item_dict_list = []
for item in items:
video_ranking = item.xpath('div[@class="num"]/text()')[0]
video_name = item.xpath('div[@class="content"]/div[@class="info"]/a/text()')[0]
#normalize-space 去掉\r\n\t
video_view_counts = item.xpath('normalize-space(div[@class="content"]/div[@class="info"]/div[@class="detail"]/span[1]/text())')
video_comments_counts = item.xpath('normalize-space(div[@class="content"]/div[@class="info"]/div[@class="detail"]/span[2]/text())')
video_composite_scores = item.xpath('div[@class="content"]/div[@class="info"]/div[@class="pts"]/div/text()')[0]
video_url = item.xpath('div[@class="content"]/div[@class="info"]/a/@href')[0]
item_dict = {
'video_ranking':video_ranking,
'video_name': video_name,
'video_view_counts': video_view_counts,
'video_comments_counts': video_comments_counts,
'video_composite_scores': video_composite_scores,
'video_url':video_url
}
item_dict_list.append(item_dict)
with open('E:\\哔哩哔哩月排行.csv','w',encoding='utf-8-sig',newline='') as f:
writer = csv.DictWriter(f,fieldnames=['video_ranking','video_name','video_view_counts','video_comments_counts','video_composite_scores','video_url'])
writer.writeheader()
writer.writerows(item_dict_list)