【发布时间】:2016-06-21 09:26:38
【问题描述】:
我是 Python 新手。从文档中提取列表时出现问题。我的源文件不是真正的 html,但它有一个标签来提取所需的数据。
我设法使用此代码提取我需要的数据
from bs4 import BeautifulSoup
url = r"E:\Python\Sources\test.review"
page = open(url)
soup = BeautifulSoup(page.read())
for review in soup.find_all(['review_text','product_name']):
tokens=review.get_text()
print tokens
但是问题在于如何打破结果,因为我并不真正熟悉在 Python 中使用列表。我尝试使用此代码,但它只返回第一个数据。我相信它,因为它引用了文件中的第一个数据。感谢您的所有反馈。
rvwTxt=soup.review_text.string
pName=soup.product_name.string
print rvwTxt
print pName
【问题讨论】:
-
请edit您的问题并将XML作为代码包含在内。
标签: python beautifulsoup