我想在以下网站上下载上面需要知道的 4 篇文章的 href:
但我无法使用 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/