Beautiful Soup - 在文档的两个部分之间搜索

发布于 2024-09-30 07:54:07 字数 524 浏览 0 评论 0原文

是否可以仅对页面上两个字符串之间包含的内容运行 Beautiful Soup?

在多次遇到使用 Yahoo Pipes 进行屏幕抓取的限制后,我开始使用 Beautiful Soup,部分原因是 Scraperwiki 上提供了托管版本。

Yahoo Pipes 中 HTML 导入块的便利之处之一是,它可以让您识别开始字符串和停止字符串,因此您可以将抓取限制在页面的特定区域。

我在 Beautiful soup 中找到了一种从/刮取特定字符串的方法:

def scrapeFrom(soup,txt,el,attr=''):
start=soup.find(text=txt)
return start.findAllNext(el,attr)

但不知道如何转储字符串下游的所有内容?

也就是说,我希望能够说“scrapeFromUntil(soup,fromText,untilText)”并且只刮掉这两个字符串之间的标签?

有什么想法如何做到这一点吗?

Is it possible to just run Beautiful Soup over content contained between two strings on a page?

Having repeatedly run up against the limitations of using Yahoo Pipes for screenscraping, I've started using Beautiful Soup, in part because a hosted version is available on Scraperwiki.

One of the handy things about the HTML Import block in Yahoo Pipes is that it lets you identify a start string and a stop string, so you can limit a scrape to a particular region of a page.

I found a way of scraping /from/ a particular string in Beautiful soup:

def scrapeFrom(soup,txt,el,attr=''):
start=soup.find(text=txt)
return start.findAllNext(el,attr)

but can't see how to dump everything downstream of a string?

That is, I want to be able to say "scrapeFromUntil(soup,fromText,untilText)" and only scrape tags between those two strings?

Any ideas how to do that?

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

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

发布评论

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

评论(1

爱已欠费 2024-10-07 07:54:07

不完全是您想要的,但根据 HTML 的格式,您可能会发现 使用 SoupStrainer 将解析仅限于文档的一部分 很有用。

Not exactly what you wanted, but depending on the format of your HTML you may find using SoupStrainer to limit parsing to only part of the document useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文