使用BeautifulSoup解析以&lt分隔线; BR&GT;标签? [英] Using BeautifulSoup to parse lines seperated by <br> tags?
问题描述
我有一个页面,看起来像这样:
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片段,只需使用正则表达式来替换&LT; BR /&GT;
其次由一个单一的换行符可选的换行,然后分裂多换行。这将导致多个个别段落,您可以手动处理。
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屋!