BeautifulSoup:跳过html元素

时间:2012-07-24 01:28:15

标签: python html dom beautifulsoup scrape

我有以下html结构:这只是它的一部分,但我认为这个片段足以解释我的问题。

<tr>
<td> Color Digest </td>
<td> AgAkAZwCJgMZ </td>
</tr>
<tr>
<td> Color Digest </td>
<td> 2,36,156,38,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, </td>
</tr>

我有以下代码来提取“Color Digest”标签的下一个兄弟

for td in soupPage.html.findAll('td'):
    if td.text == 'Color Digest':
        if td.nextSibling.text != " ":
            a = set()
            a = "[" + td.nextSibling.text.strip(",") + "]"
            print a

但我想跳过<td> AgAkAZwCJgMZ </td> 并获取<td> 2,36,156,38,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, </td>

中的值

我可以遵循的最佳美容汤机制是什么?

2 个答案:

答案 0 :(得分:0)

您可以通过这种方式实现:

import re
from bs4 import BeautifulSoup

html = """
<tr>
<td> Color Digest </td>
<td> AgAkAZwCJgMZ </td>
</tr>
<tr>
<td> Color Digest </td>
<td> 2,36,156,38,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, </td>
</tr>
"""

soup = BeautifulSoup(html)
output = soup.find_all('td', text = re.compile(" Color Digest "))[1].find_next('td').text


print(output)

答案 1 :(得分:0)

更多html(例如整个表)将有助于编写更可靠的内容。

如果您知道要排除的字符串。使用bs4 4.7.1

from bs4 import BeautifulSoup as bs

html = '''
<tr>
<td> Color Digest </td>
<td> AgAkAZwCJgMZ </td>
</tr>
<tr>
<td> Color Digest </td>
<td> 2,36,156,38,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, </td>
</tr>
'''
soup = bs(html, 'lxml')
elems = [item.text for item in soup.select('td:contains("Color Digest") + td:not(:contains("AgAkAZwCJgMZ"))')]
print(elems)

如果您不这样做,则在返回列表上使用索引

elems = [item.text for item in soup.select('td:contains("Color Digest") + td')][1]