【问题标题】:Get all values of href from a class in HTML snippet using beautifulSoup使用 beautifulSoup 从 HTML 片段中的类中获取 href 的所有值
【发布时间】:2020-04-02 17:58:52
【问题描述】:

我正在尝试构建一个网络抓取工具,但在检索 css 字段时遇到了困难。

给定 HTML sn-p,我如何使用 beautifulSoup 获取所有这些 href 值。

此外,该类是唯一用于此的。

<a class="list-group-item" href="/test1/">
<a class="list-group-item" href="/test2/">
<a class="list-group-item" href="/test3/">

我应该可以得到 test1、test2 和 test3。

有人可以帮忙吗。

【问题讨论】:

标签: python html css web-scraping beautifulsoup


【解决方案1】:

要获取所有元素,

all = soup.find_all("a", {"class": "list-group-item"}, href=True)

获取href

for ele in all:
    print(ele['href'].strip('/'))

【讨论】:

  • 非常感谢!正是我想要的。
猜你喜欢
  • 2013-12-18
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多