如何使用beautifulsoup python在HTML列表中提取文本 [英] How to extract text within HTML lists using beautifulsoup python
本文介绍了如何使用beautifulsoup python在HTML列表中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个python程序,该程序可以在html中的列表之间提取文本.我想提取诸如精装书和页数之类的信息.有人知道此操作的命令吗?
I'm trying to write a python program that can extract text between list in html. I would like to extract information like the book being hardcover and number of pages. Does anybody know the command for this operation?
<h2>Product Details</h2>
<div class="content">
<ul>
<li><b>Hardcover:</b> 156 pages</li>
<li><b>Publisher:</b> Insight Editions; Har/Pstr edition (June 18, 2013)</li>
<li><b>Language:</b> English</li>
<li><b>ISBN-10:</b> 1608871827</li>
<li><b>ISBN-13:</b> 978-1608871827</li>
要解析我使用的其他信息:
For parse other information I used:
definition in soup.findAll('span', {"class":'bb_price'}):
definition = definition.renderContents()
但不适用于这种情况.
推荐答案
通过 next_sibling
.
工作示例:
from bs4 import BeautifulSoup
data = """<h2>Product Details</h2>
<div class="content">
<ul>
<li><b>Hardcover:</b> 156 pages</li>
<li><b>Publisher:</b> Insight Editions; Har/Pstr edition (June 18, 2013)</li>
<li><b>Language:</b> English</li>
<li><b>ISBN-10:</b> 1608871827</li>
<li><b>ISBN-13:</b> 978-1608871827</li></ul></div>"""
soup = BeautifulSoup(data)
print soup.find('b', text='Hardcover:').next_sibling
print soup.find('b', text='Publisher:').next_sibling
打印:
156 pages
Insight Editions; Har/Pstr edition (June 18, 2013)
这篇关于如何使用beautifulsoup python在HTML列表中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文