我想在下面的网站上下载NEED TO KNOW上面的4篇文章的hrefs:
但我无法使用 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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句