【问题标题】:Find all the contents between two tags in python在python中查找两个标签之间的所有内容
【发布时间】:2013-09-02 02:05:09
【问题描述】:
<p>This is the first paragraph with some details</p>
<p><a href = "user123">user1</a><font>This is opening contents for user1</font></p>
<p><font>This is the contents from user1</font></p>
<font><p>This is more content from user1</p></font>
<p><a href = "user234">user2</a><font>This is opening contents for user2</font></p>
<p><font>This is the contents from user2</font></p>
<font><p>This is more content from user1</p></font>
!----There is n number of data like this-----!

这是我的html的结构。我的目标是提取用户及其内容。在这种情况下,它应该打印两个“a”标签之间的所有内容。这只是我的结构的一个例子,但在真正的 html 中,我在两个“a”标签之间有不同类型的标签。我需要一个解决方案来迭代“a”标签下的所有标签,直到找到另一个“a”标签。希望这很清楚。

我试过的代码是:

for i in soup.findAll('a'):
    while(i.nextSibling.name!='a'):
        print i.nextSibling

我返回一个无限循环。所以如果有人知道我该如何解决这个问题,请与我分享。

预期输出是:

用户名是:user1

text is : 这是为 user1 打开的内容 这是来自 user1 的内容 这是来自 user1 的更多内容

用户名是:user2

text is : 这是为 user2 打开的内容 这是来自 user2 的内容 这是来自 user2 的更多内容

等等……

【问题讨论】:

  • 您在第 2 行和第 4 行中缺少结束 &lt;/p&gt;。这是您的示例中的错误,还是真的如此?
  • 你得到一个无限循环的原因是你没有遍历i的兄弟姐妹,你每次都在看同一个兄弟姐妹。
  • @nickie -- 那是错误的......我现在已经更正了......
  • 好的,那么我认为我的解决方案有效。
  • 上次编辑后,第 4 行和第 7 行中的标签 &lt;p&gt;&lt;font&gt; 嵌套不正确。

标签: python python-2.7 beautifulsoup


【解决方案1】:

一种选择是使用find_all() 搜索每个&lt;a&gt; 标记,并为每个链接使用find_all_next() 搜索具有每个用户内容的&lt;font&gt; 标记。以下脚本提取用户名及其内容并将两者保存为列表中的元组:

from bs4 import BeautifulSoup

l = []

soup = BeautifulSoup(open('htmlfile'))
for link in soup.find_all('a'):
    s = []
    for elem in link.find_all_next(['font', 'a']):
        if elem.name == 'a':
            break
        s.append(elem.string)
    user_content = ' '.join(s)
    l.append((link.string, user_content))

它产生:

[('user1', 'This is the contents from user1 This is more content from user1'),
 ('user2', 'This is the contents from user2 This is more content from user2')]

【讨论】:

  • 感谢您的解决方案。但它只产生了一些领域。假设两个“a”标签之间有不同的标签名称,我想提取这些标签的所有内容。这是一个问题,因为我的 html 非常不一致。我正在寻找可以提取所有数据的东西,直到找到另一个“a”标签
  • @user2657822:你的意思是提取&lt;a&gt;标签之间的所有文本?为什么不将预期的输出添加到您的问题中?会更清楚。
  • @Birei-我现在已经编辑过了。希望它清楚。感谢您的建议
  • @user2657822:我已经编辑了答案。请注意,我必须修复您的示例数据,&lt;p&gt;&lt;font&gt; 标记混合在某些行中。
  • @user2657822:输出是我所期望的。
【解决方案2】:

试试这个:

from bs4 import BeautifulSoup

html="""
<p>This is the first paragraph with some details</p>
<p><a href="user123">user1</a><font>This is opening contents for user1</font></p>
<p><font>This is the contents from user1</font></p>
<font><p>This is more content from user1</p></font>
<p><a href="user234">user2</a><font>This is opening contents for user2</font></p>
<p><font>This is the contents from user2</font></p>
<font><p>This is more content from user1</p></font>
"""

soup = BeautifulSoup(html)
for i in soup.find_all('a'):
  print 'name:', i.text
  for s in [i, i.parent.find_next_sibling()]:
    while s <> None:
      if s.find('a') <> None:
        break
      print 'contents:', s.text
      s = s.find_next_sibling()

(注意:find_allfindAll 的推荐名称,它可能不适用于旧汤。与find_next_sibling 相同。)

【讨论】:

  • @nickie- 谢谢。但这只会获取下一个标签。我现在已经明确了我的要求。我希望我新编辑的问题会更清楚。
  • 那么,也修正了答案。
  • @nickie-非常感谢......它比以前更好用了。由于我正在处理大数据,我想出了另一种情况,即在“a”标签内有一个“字体”标签,其中也包含一些内容。根据您的解决方案,哪个不会显示。例如

    user1这是为 user1 开放的内容

    。我还更新了原始问题,使其清楚。谢谢
  • 再次修复。这变得太多了,我会在这里停下来。我相信你已经得到了图片。干杯...
  • 现在工作正常..我还得学习新东西...对不起,我只是在学习 python,所以有很多疑问..但现在它已清除..非常感谢..
猜你喜欢
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 2012-10-25
  • 2022-11-18
  • 2012-12-12
  • 1970-01-01
相关资源
最近更新 更多