是否可以用美丽的汤来提取多种类型的物品?

发布于 2024-11-17 16:08:12 字数 110 浏览 0 评论 0原文

我一直在查看文档,但他们没有涵盖这个问题。我正在尝试提取所有文本和所有链接,但不是单独提取。我希望它们交错以保留上下文。我想最终得到一个文本和链接的交错列表。 BeautifulSoup 可以做到这一点吗?

I've been looking at documentation and they don't cover this issue. I'm trying to extract all text and all links, but not separately. I want them interleaved to preserve context. I want to end up with an interleaved list of text and links. Is this even possible with BeautifulSoup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南巷近海 2024-11-24 16:08:12

是的,这绝对是可能的。

import urllib2
import BeautifulSoup

request = urllib2.Request("http://www.example.com")
response = urllib2.urlopen(request)
soup = BeautifulSoup.BeautifulSoup(response)
for a in soup.findAll('a'):
    print a

分解此代码片段,您正在向网站(在本例中为 Google.com)发出请求,并使用 BeautifulSoup 解析响应。您的要求是找到所有链接和文本并保留上下文。上述代码的输出将如下所示:

<a href="/"><img src="/_img/iana-logo-pageheader.png" alt="Homepage" /></a>
<a href="/domains/">Domains</a>
<a href="/numbers/">Numbers</a>
<a href="/protocols/">Protocols</a>
<a href="/about/">About IANA</a>
<a href="/go/rfc2606">RFC 2606</a>
<a href="/about/">About</a>
<a href="/about/presentations/">Presentations</a>
<a href="/about/performance/">Performance</a>
<a href="/reports/">Reports</a>
<a href="/domains/">Domains</a>
<a href="/domains/root/">Root Zone</a>
<a href="/domains/int/">.INT</a>
<a href="/domains/arpa/">.ARPA</a>
<a href="/domains/idn-tables/">IDN Repository</a>
<a href="/protocols/">Protocols</a>
<a href="/numbers/">Number Resources</a>
<a href="/abuse/">Abuse Information</a>
<a href="http://www.icann.org/">Internet Corporation for Assigned Names and Numbers</a>
<a href="mailto:[email protected]?subject=General%20website%20feedback">[email protected]</a>

Yes, this is definitely possible.

import urllib2
import BeautifulSoup

request = urllib2.Request("http://www.example.com")
response = urllib2.urlopen(request)
soup = BeautifulSoup.BeautifulSoup(response)
for a in soup.findAll('a'):
    print a

Breaking this code snippet down, you are making a request for a website (in this case Google.com) and parsing the response back with BeautifulSoup. Your requirements were to find all links and text and keep the context. The output of the above code will look like this:

<a href="/"><img src="/_img/iana-logo-pageheader.png" alt="Homepage" /></a>
<a href="/domains/">Domains</a>
<a href="/numbers/">Numbers</a>
<a href="/protocols/">Protocols</a>
<a href="/about/">About IANA</a>
<a href="/go/rfc2606">RFC 2606</a>
<a href="/about/">About</a>
<a href="/about/presentations/">Presentations</a>
<a href="/about/performance/">Performance</a>
<a href="/reports/">Reports</a>
<a href="/domains/">Domains</a>
<a href="/domains/root/">Root Zone</a>
<a href="/domains/int/">.INT</a>
<a href="/domains/arpa/">.ARPA</a>
<a href="/domains/idn-tables/">IDN Repository</a>
<a href="/protocols/">Protocols</a>
<a href="/numbers/">Number Resources</a>
<a href="/abuse/">Abuse Information</a>
<a href="http://www.icann.org/">Internet Corporation for Assigned Names and Numbers</a>
<a href="mailto:[email protected]?subject=General%20website%20feedback">[email protected]</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文