BeautifulSoup - 修改一段 HTML 中的所有链接? [英] BeautifulSoup - modifying all links in a piece of HTML?

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

问题描述

我需要能够修改 HTML 文档中的每个链接.我知道我需要使用 SoupStrainer 但我对如何实现它并不是 100% 肯定.如果有人能指导我找到好的资源或提供代码示例,我将不胜感激.

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 解释器)

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

from bs4 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(text)

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

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