1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import requests from bs4 import BeautifulSoup import csv
url = 'https://www.bilibili.com/ranking'
response = requests.get(url) html_text = response.text soup = BeautifulSoup(html_text, 'html.parser')
class Videos: def __init__(self, title, rank, score, url): self.title = title self.rank = rank self.score = score self.url = url def to_csv(self): return[self.title, self.rank, self.score, self.url] def csv_title(): return(['标题', '排名', '分数', 'URL'])
items = soup.findAll('li',{'class':'rank-item'}) videos = []
for itm in items: title = itm.find('a', {'class':'title'}).text rank = itm.find('div', {'class':'num'}).text score = itm.find('div', {'class':'pts'}).text url = itm.find('a',{'class':'title'}).get('href') v = Videos(title, rank, score, url) videos.append(v) file_name = 'Top100.csv' with open(file_name, 'w', newline='') as f: pen = csv.writer(f) pen.writerow(Videos.csv_title()) for v in videos: pen.writerow(v.to_csv())
|