我是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中使用list,因此存在如何破坏结果的问题。我尝试使用此代码,但它只返回第一个数据。我相信它,因为它引用了文件中的第一个数据。谢谢你的反馈
rvwTxt=soup.review_text.string
pName=soup.product_name.string
print rvwTxt
print pName
您可以在dict中分组,使用标记名进行分组,这样您就可以在一次过程中完成分组:
或使用两个列表组件:
相关问题 更多 >
编程相关推荐