【发布时间】:2018-04-04 07:25:10
【问题描述】:
我在我的 python 脚本中使用了一个选择器来从下面给出的一些 html 元素中获取文本。我尝试使用.text 从元素中获取Shop here cheap 字符串,但它根本不起作用。但是,当我尝试使用 .text_content() 时,它可以正常工作。
我的问题是:
.text 方法有什么问题?为什么它不能从元素中解析文本?
HTML 元素:
<div class="Price__container">
<span class="ProductPrice" itemprop="price">$6.35</span>
<span class="ProductPrice_original">$6.70</span>
Shop here cheap
</div>
我尝试了什么:
from lxml import html
tree = html.fromstring(element)
for data in tree.cssselect(".Price__container"):
print(data.text) #It doesn't work at all
顺便说一句,我不希望继续使用.text_content(),这就是为什么我希望得到任何答案来使用.text 来抓取文本。提前致谢。
【问题讨论】:
标签: python python-3.x web-scraping css-selectors