和美丽的芬达在一起

2024-03-28 15:02:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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

http://www.marketwatch.com/

但我不能把他们和芬德尔区别开来。下面的方法为我提供了一些文章,还有一些其他的,也符合这些标准。你知道吗

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篇文章?你知道吗


Tags: comhttp网站wwwarticletrendarticlesclass
1条回答
网友
1楼 · 发布于 2024-03-28 15:02:01

似乎对我很有用:

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)

相关问题 更多 >