【问题标题】:How can I extract all text between tags?如何提取标签之间的所有文本?
【发布时间】:2016-03-22 13:27:27
【问题描述】:

我想从this book中随机抽取一首诗。

使用 BeautifulSoup,我已经能够找到标题和散文。

print soup.find('div', class_="pre_poem").text
print soup.find('table', class_="poem").text

但我想找到所有的诗,然后挑一首。 我应该使用正则表达式并匹配所有之间 <h3></span></p> ?

【问题讨论】:

  • 也许find_all() ?
  • 你知道这个项目吗:BoilerPipe?
  • 永远不要使用正则表达式解析 HTML。

标签: python html regex beautifulsoup


【解决方案1】:

假设您已经有一个合适的 soup 对象可以使用,以下内容可能会帮助您入门:

poem_ids = []

for section in soup.find_all('ol', class_="TOC"):
    poem_ids.extend(li.find('a').get('href') for li in section.find_all('li'))

poem_ids = [id[1:] for id in poem_ids[:-1] if id]
poem_id = random.choice(poem_ids)

poem_start = soup.find('a', id=poem_id)
poem = poem_start.find_next()
poem_text = []

while True:
    poem = poem.next_element

    if poem.name == 'h3':
        break

    if poem.name == None:
        poem_text.append(poem.string)

print '\n'.join(poem_text).replace('\n\n\n', '\n')

首先从页面顶部的目录中提取诗歌列表。这些包含每首诗的唯一 ID。接下来选择一个随机 ID,然后根据该 ID 提取匹配的诗歌。

例如,如果选择了第一首诗,您将看到以下输出:

"The Arrow and the Song," by Longfellow (1807-82), is placed first in
this volume out of respect to a little girl of six years who used to
love to recite it to me. She knew many poems, but this was her
favourite.


I shot an arrow into the air,
It fell to earth, I knew not where;
For, so swiftly it flew, the sight
Could not follow it in its flight.


I breathed a song into the air,
It fell to earth, I knew not where;
For who has sight so keen and strong
That it can follow the flight of song?


Long, long afterward, in an oak
I found the arrow, still unbroke;
And the song, from beginning to end,
I found again in the heart of a friend.


Henry W. Longfellow.

这是通过使用 BeautifulSoup 从每个元素中提取所有文本直到找到下一个 <h3> 标记,然后删除任何多余的换行符来完成的。

【讨论】:

    【解决方案2】:

    请改用html document parser。就意外后果而言,它更安全。

    所有程序员不鼓励使用正则表达式解析 HTML 的原因是页面的 HTML 标记不是静态的,尤其是当您的源 HTML 是网页时。正则表达式更适合字符串。

    使用正则表达式需要您自担风险。

    【讨论】:

    • 页面不是静态的论点实际上适用于您无法控制的任何数据解析。我想说一个更有力的论点是被解析的数据不规则。
    • 同意你的看法。为您的评论 +1
    猜你喜欢
    • 2017-08-15
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多