【问题标题】:Scraping all h1 tags contents with beautiful soup用漂亮的汤刮掉所有 h1 标签内容
【发布时间】:2015-11-19 23:34:45
【问题描述】:

我正在尝试用漂亮的汤来抓取一些评论数据,它只会让我抓取一个元素:

BASE_URL = "http://consequenceofsound.net/'category/reviews/album-reviews/"
html = urlopen(BASE_URL + section_url).read()
soup = BeautifulSoup(html, "lxml")
meta = soup.find("div", {"class": "content"}).h1
wordage = [s.contents for s in meta]

这将让我从该页面中获取一个评论标题。但是,当我将 find 更改为 find_all 时,我无法在这一行识别 h1,所以我得到了一些类似这样的代码:

meta = soup.find("div", {"class": "content"})
wordage = [s.h1 for s in meta]

我无法找到隔离内容的方法。

【问题讨论】:

  • 您希望wordage 是什么?另外,您能否展示一个非常简单的 html 示例?
  • 你能告诉我们你到底想要什么

标签: python web-scraping beautifulsoup nlp screen-scraping


【解决方案1】:
meta = soup.find_all("div", {"class": "content"})

wordage = [s.h1 for s in meta if s.h1 not in ([], None)]
link = [s.a['href'] for s in wordage]

请注意添加了“不在”语句。有时似乎是空的,并且没有类型的列表被添加到“汤”中,所以这是一个重要的措施。

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 1970-01-01
    • 2021-06-25
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多