利用python获取B站排名前100视频

暂定于今天仍然可以使用
无需做任何修改
可以现学现用

import requests
from bs4 import BeautifulSoup
import time
url = 'https://www.bilibili.com/ranking?spm_id_from=333.334.b_62616e6e65725f6c696e6b.11'
user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3676.400 QQBrowser/10.4.3469.400'
headers = {
    'User-Agent': user_agent
}
response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
soup = BeautifulSoup(response.text, 'lxml')
timeLocal = time.strftime('%Y-%m-%d', time.localtime(time.time()))
for tag in soup.find_all('li', class_='rank-item'):
    titles = tag.find('a', class_="title").get_text()  # 标题
    urls_clss = tag.find('div', class_='info')
    playNumbers = tag.find('span', class_='data-box').get_text()  # 播放数
    div_class = tag.find('div', class_="detail")
    urls = urls_clss.find('a').get('href')
    Urls = urls.split('//')[1]  # 链接
    span = div_class.findAll('span')
    peoplecounts = span[1].get_text()  # 评论数
    auoths = span[2].get_text()  # 作者
    print("up:", auoths, "标题:", titles, "播放数:",
          playNumbers, "评论数:", peoplecounts, "链接:", Urls)
    with open(timeLocal+".txt", "a", encoding='utf-8') as f:
        f.write('\n')
        for i in range(1, 2):
            f.write("up:")
            f.write(auoths)
            f.write("  ")
            f.write("标题:")
            f.write(titles)
            f.write("  ")
            f.write("播放数:")
            f.write(playNumbers)
            f.write("  ")
            f.write("评论数:")
            f.write(peoplecounts)
            f.write("  ")
            f.write("链接:")
            f.write(Urls)
print("bilibili前100名—————写入本地已完成!", "当前时间为:", timeLocal)

B站链接直链
创作不易,点个赞吧!!

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处
本文链接:https://blog.csdn.net/wsad861512140

你可能感兴趣的:(python的教学)