测试BeautifulSoup中的标记中是否存在属性

LB4*_*B40 60 python beautifulsoup

我想获取<script>文档中的所有标记,然后根据某些属性的存在(或不存在)处理每个标记.

例如,对于每个<script>标签,如果属性for存在,则执行某些操作; 否则,如果属性bar存在,则执行其他操作.

这是我目前正在做的事情:

outputDoc = BeautifulSoup(''.join(output))
scriptTags = outputDoc.findAll('script', attrs = {'for' : True})
Run Code Online (Sandbox Code Playgroud)

但是这样我<script>for属性过滤所有标签......但是我丢失了其他标签(那些没有for属性的标签).

Luc*_* S. 82

如果我理解得很好,你只需要所有的脚本标签,然后检查它们中的一些属性?

scriptTags = outputDoc.findAll('script')
for script in scriptTags:
    if script.has_attr('some_attribute'):
        do_something()        
Run Code Online (Sandbox Code Playgroud)

  • 请更新这篇文章,不推荐使用has_key.请改用has_attr. (10认同)
  • 要检查可用属性,必须使用python dict方法,例如:script.has_key('some_attribute') (5认同)
  • 可悲的是,对我不起作用。也许这样`soup_response.find('err').string is not None` 也可以用于其他属性...... (3认同)

mia*_*iah 29

为了将来参考,has_key已被弃用是beautifulsoup 4.现在你需要使用has_attr

scriptTags = outputDoc.findAll('script')
  for script in scriptTags:
    if script.has_attr('some_attribute'):
      do_something()  
Run Code Online (Sandbox Code Playgroud)


Pad*_*ham 20

你不需要任何lambda按属性过滤,你只需要设置some_attribute=True等..:

script_tags = soup.find_all('script', some_attribute=True)

# or

script_tags = soup.find_all('script', {"some-data-attribute": True})
Run Code Online (Sandbox Code Playgroud)

你也可以使用re和find/find_all等..:

soup = bs4.BeautifulSoup(html)

# Find all with a specific attribute

tags = soup.find_all(src=True)
tags = soup.select("[src]")

# Find all meta with either name or http-equiv attribute.

soup.select("meta[name],meta[http-equiv]")

# find any tags with any name or source attribute.

soup.select("[name], [src]")

# find first/any script with a src attribute.

tag = soup.find('script', src=True)
tag = soup.select_one("script[src]")

# find all tags with a name attribute beginning with foo
# or any src beginning with /path
soup.select("[name^=foo], [src^=/path]")

# find all tags with a name attribute that contains foo
# or any src containing with whatever
soup.select("[name*=foo], [src*=whatever]")

# find all tags with a name attribute that endwith foo
# or any src that ends with  whatever
soup.select("[name$=foo], [src$=whatever]")
Run Code Online (Sandbox Code Playgroud)


小智 14

如果您只需要获取带有属性的标记,则可以使用lambda:

soup = bs4.BeautifulSoup(YOUR_CONTENT)
Run Code Online (Sandbox Code Playgroud)
  • 带属性的标签
tags = soup.find_all(lambda tag: 'src' in tag.attrs)
Run Code Online (Sandbox Code Playgroud)

要么

tags = soup.find_all(lambda tag: tag.has_attr('src'))
Run Code Online (Sandbox Code Playgroud)
  • 具有属性的特定标记
tag = soup.find(lambda tag: tag.name == 'script' and 'src' in tag.attrs)
Run Code Online (Sandbox Code Playgroud)
  • 等......

认为它可能有用.

  • 优雅的解决方案! (2认同)