gpt4 book ai didi

python - 使用 BeautifulSoup FindAll 进行网页抓取

转载 作者:行者123 更新时间:2023-11-28 22:28:58 25 4
gpt4 key购买 nike

我想在以下网站上下载 NEED TO KNOW 上面 4 篇文章的 hrefs:

http://www.marketwatch.com/

但我无法用 FindAll 唯一地识别它们。以下方法为我提供了同样符合这些标准的文章,以及其他一些文章。

trend_articles  = soup1.findAll("a", {"class": "link"})
href= article.a["href"]

trend_articles = soup1.findAll("div", {"class": "content--secondary"})
href= article.a["href"]

有人有什么建议吗,我怎样才能得到这 4 篇文章,而且只有这 4 篇文章?

最佳答案

这似乎对我有用:

from bs4 import BeautifulSoup
import requests

page = requests.get("http://www.marketwatch.com/").content
soup = BeautifulSoup(page, 'lxml')
header_secondare = soup.find('header', {'class': 'header--secondary'})
trend_articles = header_secondare.find_next_siblings('div', {'class': 'group group--list '})[0].findAll('a')

trend_articles = [article.contents[0] for article in trend_articles]
print(trend_articles)

关于python - 使用 BeautifulSoup FindAll 进行网页抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43314445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com