【发布时间】:2022-07-16 23:15:08
【问题描述】:
这是我的代码:
soup = BeautifulSoup("<html><body>BLAR fff11 £ </body></html>", 'html.parser')
for z in soup.find_all(text=re.compile('£')):
print(z)
由于某种原因没有返回任何内容,但是如果我更改示例 html 和我的 find 语句中的特殊字符,它会起作用:
soup = BeautifulSoup("<html><body>BLAR fff11 pound </body></html>", 'html.parser')
for z in soup.find_all(text=re.compile('pound')):
print(z)
输出为:BLAR fff11 磅
有谁知道我哪里出错了,以及如何找到带有特殊字符的字符串?
谢谢
【问题讨论】:
-
&和;可能是正则表达式中的特殊字符吗?至少,我会反斜杠他们 -
默认情况下,BeautifulSoup 会将您的输入
&pound;转换为£。如果您想更改此设置,请查看:beautiful-soup-4.readthedocs.io/en/latest/#output-formatters
标签: python beautifulsoup