Python 爬虫实战
基于 python 的 requests、BeautifulSoup4 第三方库,对哔站全站范围热门视频排行榜前一百实时爬取。
import requests from bs4 import BeautifulSoup file = open("hot100.txt", "w") url = "https://www.bilibili.com/v/popular/rank/all/" header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.55'} res = requests.get(url, headers=header) if res.status_code != 200: print(res.status_code) raise Exception("error") html = res.text soup = BeautifulSoup(html, "html.parser") links = soup.find("div", id="app")\ .find("ul", class_="rank-list")\ .find_all("li", class_="rank-item") for link in links: link = link.find("div", class_="info").find("a", class_="title") name = link.get_text() href = "https:" + link["href"] print("[%s](%s)" % (name, href)) try: file.write("[%s](%s)\n" % (name, href)) except UnicodeEncodeError: file.write("[Error](%s)\n" % href) file.close()
本文作者:SRIGT
本文链接:https://www.cnblogs.com/SRIGT/p/17205928.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步