BeautifulSoup - 修改在一块HTML的各个环节? [英] BeautifulSoup - modifying all links in a piece of HTML?

查看:140
本文介绍了BeautifulSoup - 修改在一块HTML的各个环节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够修改HTML文档中的每一个环节。我知道我需要使用 SoupStrainer ,但我不是如何实现它正面100%。如果有人可以直接我一个很好的资源或者提供code为例,这将会是非常AP preciated。

I need to be able to modify every single link in an HTML document. I know that I need to use the SoupStrainer but I'm not 100% positive on how to implement it. If someone could direct me to a good resource or provide a code example, it'd be very much appreciated.

感谢。

推荐答案

也许像这样的工作吗? (我没有在我面前一个Python间preTER,可惜)

Maybe something like this would work? (I don't have a Python interpreter in front of me, unfortunately)

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<p>Blah blah blah <a href="http://google.com">Google</a></p>')
for a in soup.findAll('a'):
  a['href'] = a['href'].replace("google", "mysite")

result = str(soup)

这篇关于BeautifulSoup - 修改在一块HTML的各个环节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆