【问题标题】:Webscraping with BeautifulSoup FindAll使用 BeautifulSoup FindAll 进行网页抓取
【发布时间】:2017-04-10 03:10:19
【问题描述】:

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

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 篇文章?

【问题讨论】:

  • 我找到了一条可能的路径,它从顶部开始:trend_articles = soup1.findAll("div", {"class": "element2 element--article is-lead "}) 然后使用 href= article.div.div.ul.li.a["href"] 但这会引导我进入第一个 ul,但我需要进入第二个 ul。
  • 基本上,如果我可以沿着树导航,那会很有帮助。

标签: python web-scraping findall


【解决方案1】:

这似乎对我有用:

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)

【讨论】:

  • 哇,这看起来比我尝试做的要简单得多。非常感谢!!!
  • @Niccola Tartaglia 欢迎您!祝你好运!
猜你喜欢
  • 2018-08-02
  • 1970-01-01
  • 2020-10-04
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 2020-09-13
相关资源
最近更新 更多