【问题标题】:How do I reference attribute when using python/beautifulSoup使用 python/beautifulSoup 时如何引用属性
【发布时间】:2021-07-21 04:11:37
【问题描述】:

看下面的代码sn-p。我发现“_ngcontent...”是有角度的,但是如何使用该属性引用这个div

通常,我会告诉soup 给我div 以及类/id/名称等,但我不知道如何引用这个_ng####

<div _ngcontent-bpi-q1346="" class="grid-row ng-star-inserted">

【问题讨论】:

标签: python-3.x selenium-webdriver beautifulsoup


【解决方案1】:

您可以使用.attrs方法检查标签属性中是否存在_ngcontent-bpi-q1346

for tag in soup.find_all(
    lambda tag: tag.name == "div" and "_ngcontent-bpi-q1346" in tag.attrs
):
    print(tag)

或者:将具有属性名称的 Dictionary 传递给 attrs= 参数(docs 中的最后一个示例的第二个)。

# Using `True` as the value since `_ngcontent-bpi-q1346` doesn't have a value
for tag in soup.find_all("div", attrs={"_ngcontent-bpi-q1346": True}):
    print(tag)

【讨论】:

  • 嗨 MendelG - 我使用了第二个选项,它就像一个魅力。非常感谢!
猜你喜欢
  • 2017-08-17
  • 2017-08-28
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 2022-11-17
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多