当前位置: 首页> 健康> 母婴 > selenium 爬取今日头条

selenium 爬取今日头条

时间:2025/7/11 15:09:19来源:https://blog.csdn.net/this_is_id/article/details/139150900 浏览次数:0次

由于今日头条网页是动态渲染,再加上各种token再验证,因此直接通过API接口获取数据难度很大,本文使用selenium来实现新闻内容爬取。

selenium核心代码

知识点:

  • 代码中加了很多的异常处理,保证错误后重试,提高稳定性
  • EdgeChromiumDriverManager().install()自动下载浏览器驱动,避免浏览器更新后驱动版本不对的问题
  • 使用driver.refresh()driver.close()driver.quit()防止占用内存过多
  • 使用--disable-extensions禁用插件,避免插件可能带来的影响
  • 使用--inprivate打开无痕模式,这里遇到一个很烦的问题,用户登录同步问题,无痕模式可以避免
    在这里插入图片描述
from webdriver_manager.microsoft import EdgeChromiumDriverManagerdef get_html_by_selenium(url):print("开始:", url)options = webdriver.EdgeOptions()# 启用'禁用浏览器正在被自动化程序控制的提示'启动参数options.add_experimental_option("excludeSwitches", ["enable-automation"])# 禁用插件options.add_argument("--disable-extensions")# 无痕模式options.add_argument('--inprivate')count = 0driver = Nonewhile count < 10:try:driver = webdriver.Edge(service=Service(executable_path=EdgeChromiumDriverManager().install()),options=options)# 最小化driver.minimize_window()time.sleep(1)driver.get(url)breakexcept WebDriverException as e:print(e)count += 1time.sleep(3)continueexcept ConnectionError as e:print(e)count += 1time.sleep(3)continueif driver is None:returntime.sleep(10)try:html = driver.page_source# 防止内存泄露driver.refresh()try:driver.close()except WebDriverException:passdriver.quit()return htmlexcept NoSuchWindowException:return

新闻列表解析代码

URL示例:

https://www.toutiao.com/c/user/token/MS4wLjABAAAA6Ftyf-tftfbjp1u_TEz6kpY77ZlPaYRV0UsfXkF2UsM/?tab=article

这里比较简单,拿到了新闻标题和url,HTML解析过程中可能遇到浏览器中渲染的html结构和真实请求到的html结构不一样,要以真实拿到的html内容为准

url = f"https://www.toutiao.com/c/user/token/{USER_TOKEN}/?tab=article"
html = get_html_by_selenium(url)
soup = BeautifulSoup(html, "html.parser")for article in soup.find_all("div", attrs={"class": "profile-article-card-wrapper"}):a = article.find("a")news_title = a["title"]url = a["href"]content, news_time = parse_and_save_news(url)

新闻内容解析代码

相对比较简单,忽略了图片的解析,最终获得新闻的内容和新闻时间

def parse_and_save_news(url):html = get_html_by_selenium(url)if not html:returnsoup = BeautifulSoup(html, "html.parser")article_content = soup.find("div", attrs={"class": "article-content"})if article_content is None:returnarticle_meta = soup.find("div", attrs={"class": "article-meta"})time_string = article_meta.find("span", attrs=None).textnews_time = datetime.strptime(time_string, "%Y-%m-%d %H:%M")article = article_content.articlenew_soup = BeautifulSoup("<html><body></body></html>", "html.parser")body = new_soup.bodyfor p in article.find_all("p"):body.append(BeautifulSoup(f"<p>{p.text}</p>", "html.parser"))content = new_soup.prettify()return content, news_time
关键字:selenium 爬取今日头条

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: