本文介绍了我在哪里可以找到一些与QUOT;世界你好" - 简单美味的汤的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用美丽的汤做一个很简单的更换。比方说,我想访问所有A标签页,并追加?富他们的HREF。可有人张贴或链接到如何做类似的东西简单的例子吗?


解决方案

 从BeautifulSoup进口BeautifulSoup汤= BeautifulSoup('''
< HTML和GT;
   < HEAD><标题>测试与LT; /标题>< /头>
   <身体GT;
     &所述; A HREF =htt​​p://foo.com/>富&下; / A>
     < A HREF =htt​​p://bar.com/bar>酒吧和LT; / A>
   < /身体GT;
< / HTML>''')在soup.findAll('A')链接:#寻找所有链接
    链接['的href'] =链接['的href'] +'?富'打印汤

这将显示:

 < HTML和GT;
< HEAD><标题>测试与LT; /标题>< /头>
<身体GT;
&所述; A HREF =htt​​p://foo.com/?foo>富&下; / A>
< A HREF =htt​​p://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.

这篇关于我在哪里可以找到一些与QUOT;世界你好&QUOT; - 简单美味的汤的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:43