BeautifulSoup python 解析html文件
我正在使用 BeautifulSoup 将 html 文件中的所有逗号替换为 ‚
。这是我的代码:
f = open(sys.argv[1],"r")
data = f.read()
soup = BeautifulSoup(data)
comma = re.compile(',')
for t in soup.findAll(text=comma):
t.replaceWith(t.replace(',', '‚'))
除非 html 文件中包含一些 javascript,否则此代码有效。在这种情况下,它甚至会替换 javascript 代码中的逗号(,)。这不是必需的。我只想替换html文件的所有文本内容。
I am using BeautifulSoup to replace all the commas in an html file with ‚
. Here is my code for that:
f = open(sys.argv[1],"r")
data = f.read()
soup = BeautifulSoup(data)
comma = re.compile(',')
for t in soup.findAll(text=comma):
t.replaceWith(t.replace(',', '‚'))
This code works except when there is some javascript included in the html file. In that case it even replaces the comma(,) with in the javascript code. Which is not required. I only want to replace in all the text content of the html file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
soup.findall
可以采取可调用:soup.findall
can take a callable: