parsing - 如何通过lxml检索标签标签内的文本?
问题描述
我正在使用 lxml 来获取标签内的文本,并以这种方式进行
xpaths_for_questions_lxml = []
for tag in self.tree.iter():
try:
if tag.text and utils.is_question(tag.text.strip()):
xpaths_for_questions_lxml.append(self.tree.getpath(tag))
except Exception as e:
self.logger.debug(traceback.format_exc())
raise Exception
如果语句有问号,is_question 模块返回 true
但是当标签类型为标签时,tag.text 属性为空,即使实际网页中的标签标签内有文本,它也不会显示任何文本。
为什么标签标签不显示任何文本内容?或者需要做任何额外的事情来通过标签标签获取?
EDIT1:我的问题是,我正在遍历 dom 树中的所有孩子,但为什么标签内的文本没有显示?
解决方案
如果你想得到问题,你可以试试
r = requests.get('https://www.amctheatres.com/faqs/movie-info')
source = html.fromstring(r.text)
questions = source.xpath('//label[@itemprop="text"]/text()')
或者
questions = [label.text_content() for label in source.xpath('//label[@itemprop="text"]')]
注意label.text_content()
应该使用而不是label.text
因为label
节点包含多个子文本节点
print(questions)
#['Does the runtime shown for each movie include trailers?', 'Where can I find MPAA movie ratings information?', 'What does advertised showtime mean?', 'What movies are playing right now at AMC?', 'What movies are coming soon to AMC?', 'How can I find movie times at AMC?']
推荐阅读
- scala - Aux 模式是否提供比类型参数更好的类型安全性?
- c# - 如何使用堆中的类型对象定位结构实例的虚拟方法?
- vba - 如何检查密件抄送字段是否为空
- angular - 错误:未捕获(承诺中):TypeError:this.feed.load 不是函数 TypeError:this.feed.load 不是函数
- sql-server - SQL Server 代理作业未调用 Powershell 脚本
- matrix - 计算并显示 powerbi 中 1 个类别总数的百分比
- javascript - 在 React js 中传递多个道具的最佳方法
- node.js - 使用 Node 为 AAD 应用程序创建用户访问令牌
- laravel - 照片上传失败。Laravel 急流
- apache-spark - 了解 PySpark 中的 RDD(来自并行化)