更新时间:2022-12-08 15:01:44
从BeautifulSoup进口BeautifulSoup汤= BeautifulSoup('''
< HTML和GT;
< HEAD><标题>测试与LT; /标题>< /头>
<身体GT;
&所述; A HREF =http://foo.com/>富&下; / A>
< A HREF =http://bar.com/bar>酒吧和LT; / A>
< /身体GT;
< / HTML>''')在soup.findAll('A')链接:#寻找所有链接
链接['的href'] =链接['的href'] +'?富'打印汤
这将显示:
< HTML和GT;
< HEAD><标题>测试与LT; /标题>< /头>
<身体GT;
&所述; A HREF =http://foo.com/?foo>富&下; / A>
< A HREF =http://bar.com/bar?foo>酒吧和LT; / A>
< /身体GT;
< / HTML>
借助文档也有一定的examples更改属性的。它是一个广泛的教程,涵盖BeautifulSoup的所有常见的方面。我不知道什么是从文档丢失,也许你应该澄清一下。
I'd like to do a very simple replacement using Beautiful Soup. Let's say I want to visit all A tags in a page and append "?foo" to their href. Can someone post or link to an example of how to do something simple like that?
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('''
<html>
<head><title>Testing</title></head>
<body>
<a href="http://foo.com/">foo</a>
<a href="http://bar.com/bar">Bar</a>
</body>
</html>''')
for link in soup.findAll('a'): # find all links
link['href'] = link['href'] + '?foo'
print soup
That prints:
<html>
<head><title>Testing</title></head>
<body>
<a href="http://foo.com/?foo">foo</a>
<a href="http://bar.com/bar?foo">Bar</a>
</body>
</html>
The documentation also has some examples for changing attributes. It is an extensive tutorial that covers all common aspects of BeautifulSoup. I don't know what is missing from the documentation, maybe you should clarify.