python - BeautifulSoup:从子节点中提取值(value)

标签 python python-2.7 beautifulsoup

我有以下 html:

<td class="section">
    <div style="margin-top:2px; margin-bottom:-10px; ">
    <span class="username"><a href="user.php?id=xx">xxUsername</a></span>
    </div>
    <br>
<span class="comment">
A test comment
</span>
</td>

我只想检索 SPAN 标记中的 xxUsername 和评论文本。到目前为止,我已经这样做了:

results = soup.findAll("td", {"class" : "section"})

它确实获取了我上面提到的模式的所有 html block 。现在我想在一个循环中检索所有子值?是否可以?如果不是那么我如何获取子节点信息?

最佳答案

你可以尝试这样的事情。它基本上执行您上面所做的 - 首先遍历所有 section-classed td,然后遍历其中的所有 span 文本。这将打印出类,以防万一您需要更严格的限制:

In [1]: from bs4 import BeautifulSoup

In [2]: html = # Your html here

In [3]: soup = BeautifulSoup(html)

In [4]: for td in soup.find_all('td', {'class': 'section'}):
   ...:     for span in td.find_all('span'):
   ...:         print span.attrs['class'], span.text
   ...:         
['username'] xxUsername
['comment'] 
A test comment

或者使用比必要的更复杂的单行代码将所有内容存储回您的列表中:

In [5]: results = [span.text for td in soup.find_all('td', {'class': 'section'}) for span in td.find_all('span')]

In [6]: results
Out[6]: [u'xxUsername', u'\nA test comment\n']

或者在同一个主题上,字典的键是类的元组,值是文本本身:

In [8]: results = dict((tuple(span.attrs['class']), span.text) for td in soup.find_all('td', {'class': 'section'}) for span in td.find_all('span'))

In [9]: results
Out[9]: {('comment',): u'\nA test comment\n', ('username',): u'xxUsername'}

假设这个更接近你想要的,我建议重写为:

In [10]: results = {}

In [11]: for td in soup.find_all('td', {'class': 'section'}):
   ....:     for span in td.find_all('span'):
   ....:         results[tuple(span.attrs['class'])] = span.text
   ....:         

In [12]: results
Out[12]: {('comment',): u'\nA test comment\n', ('username',): u'xxUsername'}

关于python - BeautifulSoup:从子节点中提取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14543767/

相关文章:

python - Django表单完整性错误: foreign key with unique field,唯一约束失败

Python:时间输入验证

python - PyCluster 无法安装包

python - 有没有办法解决 selenium 浏览器中的 cookie 和隐私弹出窗口?

python - 使用 Pandas read_csv 读取 CSV 文件时出现 parsers.pyx 错误

python - 如何使用PyAutoGUI检测RGB值

python - python logging.handlers.RotatingFileHandler 是否允许创建组可写日志文件?

python - 为什么重写 __contains__ 会破坏 OrderedDict.keys?

python - ReadTimeout : HTTPSConnectionPool(host ='' , 端口=443) : Read timed out. (读取超时=10)

python - 如何使用 Python 和 Beautiful Soup 从框架中抓取信息