简体   繁体   English

获取与特定属性值匹配但与任何标签或属性名称匹配的所有元素(使用BeautifulSoup)

[英]Get all elements that match a specific attribute value, but match any tag or attribute name with BeautifulSoup

Is it possible to get all elements that match a specific attribute value, but match any tag or attribute name with BeautifulSoup. 是否可以获取所有与特定属性值匹配但与任何标签或属性名称匹配的元素,都应使用BeautifulSoup。 If so does anyone know how to do it? 如果是这样,有人知道该怎么做吗?

Here's an example of how I'm trying to do it 这是我尝试如何做的一个例子

from bs4 import BeautifulSoup
import requests

text_to_match = 'https://s3-ap-southeast-2.amazonaws.com/bettss3/images/003obzt0t_w1200_h1200.jpg'
url = 'https://www.betts.com.au/item/37510-command.html?colour=chocolate'
r = requests.get(url)
bs = BeautifulSoup(r.text, features="html.parser")
possibles = bs.find_all(None, {None: text_to_match})
print(possibles)

This gives me an empty list []. 这给了我一个空列表[]。

If I replace {None: text_to_match} with {'href': text_to_match} this example will give some results as expected. 如果我将{None: text_to_match}替换为{'href': text_to_match}本示例将提供一些预期的结果。 I'm trying to figure out how to do this without specifying the attribute's name, and only matching the value. 我试图弄清楚如何在不指定属性名称的情况下,仅匹配值的情况下执行此操作。

You can try to find_all with no limitation and filter those who doesn't correspond to your needs, as such 您可以尝试没有限制的find_all并过滤那些与您的需求不符的内容,例如

text_to_match = 'https://s3-ap-southeast-2.amazonaws.com/bettss3/images/003obzt0t_w1200_h1200.jpg'
url = 'https://www.betts.com.au/item/37510-command.html?colour=chocolate'
r = requests.get(url)
bs = BeautifulSoup(r.text, features="html.parser")
tags = [tag for tag in bs.find_all() if text_to_match in str(tag)]
print(tags)

this sort of solution is a bit clumsy as you might get some irrelevant tags, you make your text a bit more tag specific by: 这种解决方案有点笨拙,因为您可能会得到一些不相关的标签,您可以通过以下方式使文本更具体一些:

text_to_match = r'="https://s3-ap-southeast-2.amazonaws.com/bettss3/images/003obzt0t_w1200_h1200.jpg"'

which is a bit closer to the str representation of a tag with attribute 这更接近具有属性的标签的str表示形式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python Beautifulsoup匹配字符串的元素,等于标签或属性键或属性值 - python Beautifulsoup match element by string equal to tag or attribute key or attribute value 使用 BeautifulSoup 根据 name 属性获取属性值 - Get an attribute value based on the name attribute with BeautifulSoup BeautifulSoup:获取所有包含某个属性的元素 - BeautifulSoup: Get all elements that contain a certain attribute 如果属性名称重复,如何使用BeautifulSoup获取属性值 - How to use BeautifulSoup to get attribute value if the attribute name duplicated 如何使用BeautifulSoup在Python中获取特定的标签属性文本? - How to get a specific tag attribute text in Python with BeautifulSoup? 如何使用 BeautifulSoup 获取标签属性名称 - How can I get the tag attribute name using BeautifulSoup 如何使用正则表达式检索非标准关键字属性的值以将属性值与 beautifulsoup 匹配? - How can retrieve the value of a non-standard keyword attribute using regex to match attribute's value with beautifulsoup? 使用 Beautifulsoup 获取特定属性 - Get specific attribute using Beautifulsoup 获取与 class 名称匹配的所有元素 - Get all elements that match a class name BeautifulSoup:按名称查找与一个标签匹配的任何标签,或按属性查找另一个标签 - BeautifulSoup: Find any tag that matches one tag by name, or another tag by attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM