使用BeautifulSoup解析由< br>分隔的行标签? [英] Using BeautifulSoup to parse lines separated by <br> tags?

查看:103
本文介绍了使用BeautifulSoup解析由< br>分隔的行标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的页面:

I have a page that looks like this:

Company A<br />
123 Main St.<br />
Suite 101<br />
Someplace, NY 1234<br />
<br />
<br />
<br />
Company B<br />
456 Main St.<br />
Someplace, NY 1234<br />
<br />
<br />
<br />

有时,有两个而不是三个"br"标签将条目分隔开.我将如何使用BeautifulSoup来解析此文档并提取字段?我很困惑,因为我需要的文本位没有包含在我可以简单地迭代通过的段落(或类似)标记中.

Sometimes there are two rather than three "br" tags separating the entries. How would I use BeautifulSoup to parse through this document and extract the fields? I'm stumped because the bits of text that I need are not contained in paragraph (or similar) tags that I can simply iterate through.

推荐答案

一旦有了此HTML片段,只需使用正则表达式将<br />替换为单个换行符,然后将可选的换行符替换为单个换行符,然后拆分为多个换行符.这将导致您可以手动处理多个单独的段落.

Once you have this HTML fragment, just use a regex to replace <br /> followed by an optional newline by a single newline, then split on multiple newlines. This should result in multiple individual paragraphs which you can process manually.

这篇关于使用BeautifulSoup解析由&lt; br&gt;分隔的行标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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