Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BeautifulSoup,通过正则表达式获得比预期更多的回报_Python_Regex_Beautifulsoup - Fatal编程技术网

Python BeautifulSoup,通过正则表达式获得比预期更多的回报

Python BeautifulSoup,通过正则表达式获得比预期更多的回报,python,regex,beautifulsoup,Python,Regex,Beautifulsoup,使用BeautifulSoup,我有以下行: dimensions = SOUP.select(".specs__title > h4", text=re.compile(r'Dimensions')) 但是,它返回的不仅仅是具有“维度”文本的标记,如这些结果所示: [<h4>Dimensions</h4>, <h4>Details</h4>, <h4>Warranty / Certifications</h4>]

使用BeautifulSoup,我有以下行:

dimensions = SOUP.select(".specs__title > h4", text=re.compile(r'Dimensions'))
但是,它返回的不仅仅是具有“维度”文本的标记,如这些结果所示:

[<h4>Dimensions</h4>, <h4>Details</h4>, <h4>Warranty / Certifications</h4>]
[尺寸、细节、保修/认证]

我是否在汤的工作方式中错误地使用了正则表达式?

选择
界面没有
文本
关键字。在我们进一步讨论之前,假设您使用的是BeautifulSoup 4.7+

如果您想按文本过滤,您可能可以执行以下操作:

dimensions = SOUP.select(".specs__title > h4:contains(Dimensions)")
有关
:contains()
伪类实现的更多信息,请访问:


编辑:为了澄清,目前没有办法将正则表达式直接合并到
select
调用中。要使用正则表达式,您必须在事后过滤元素。将来可能会有一种方法通过一些定制的伪类来使用正则表达式,但目前在Soup Sieve(Beauty Soup在4.7+中的select实现)中没有这种功能。

选择
界面没有
文本
关键字。在我们进一步讨论之前,假设您使用的是BeautifulSoup 4.7+

如果您想按文本过滤,您可能可以执行以下操作:

dimensions = SOUP.select(".specs__title > h4:contains(Dimensions)")
有关
:contains()
伪类实现的更多信息,请访问:


编辑:为了澄清,目前没有办法将正则表达式直接合并到
select
调用中。要使用正则表达式,您必须在事后过滤元素。将来可能会有一种方法通过一些自定义的伪类来使用regex,但目前在Soup Sieve(Beauty Soup在4.7+中的select实现)中没有这种功能。

谢谢!那很有效!同时感谢您参考:contains()文档!没问题。我是“汤筛”的作者/维护者,所以让人们知道它能做什么:)。谢谢!那很有效!同时感谢您参考:contains()文档!没问题。我是“汤筛”的作者/维护者,所以让人们知道它能做什么:)。