[英]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.