- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 python 结合 lxml libary 编写了一个脚本,用于从一大块 html 元素
中解析一些 price
(本例中为 80 和 100)。我使用 xpaths 来完成这项工作。当我使用 .fromstring()
时,我在下面的抓取工具中使用的两个 xpath
都可以完美地工作。但是,当我选择使用从 lxml.etree
导入的 HTML
时,xpath containsig contains()
表达式会失败。事实证明,当我在抓取器中使用多个类
名称时,它可以工作,但是当从复合类名称
中选择一个单个类名称
时,它就可以了抛出错误。
如何在不使用复合类名
的情况下处理这种情况;而是使用遵循.contains()
模式的单个类名
或东西?
这是我的尝试:
from lxml.etree import HTML
elements =\
"""
<li class="ProductPrice">
<span class="Regular Price">80.00</span>
</li>
<li class="ProductPrice">
<span class="Regular Price">100.00</span>
</li>
"""
root = HTML(elements)
for item in root.findall(".//*[@class='ProductPrice']"):
# regular = item.find('.//span[@class="Regular Price"]').text
regular = item.find('.//span[contains(@class,"Regular")]').text
print(regular)
顺便说一句,上面脚本中使用的注释掉的xpath
工作正常。但无法使用 .contains()
表达式,它会引发以下错误:
Traceback (most recent call last):
File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\SO.py", line 15, in <module>
regular = item.find('.//span[contains(@class,"Regular")]').text
File "src\lxml\etree.pyx", line 1526, in lxml.etree._Element.find
File "src\lxml\_elementpath.py", line 311, in lxml._elementpath.find
File "src\lxml\_elementpath.py", line 300, in lxml._elementpath.iterfind
File "src\lxml\_elementpath.py", line 283, in lxml._elementpath._build_path_iterator
File "src\lxml\_elementpath.py", line 229, in lxml._elementpath.prepare_predicate
SyntaxError: invalid predicate
最后一件事:我不想使用复合类名
,因为很少有网站动态生成它们。谢谢。
最佳答案
.find()
仅支持基本的 xpath。
尝试.xpath()
。
示例(未经测试)...
regular = item.xpath('.//span[contains(@class,"Regular")]')[0].text
参见http://lxml.de/xpathxslt.html了解更多详情。
关于python - 即使使用正确的 xpath,Scraper 也会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50649171/
编辑:为什么这被否决了?我真的不知道...顺便说一句 ../不起作用,因为我不想要 Table 的父级但实际上想要 ../td+1 我不知道这是否可能? 嗨,大家好。 我手头有一个相当复杂的问题..
我很难找到需要单击的输入(复选框)元素的 xpath。我正在尝试使用其他跨度元素来定位它。元素包含 Angular 属性,不知道这是否重要? 元素的结构如下: Company name
我正在尝试构建一个包含许多 XPath 作为参数的 DSL。我是 XPath 的新手,我需要一个 XPath 语法中从未使用过的字符,这样我就可以在脚本的一行中分隔 n 个 XPath。我的问题:哪些
使用xpath在父标签内找到特定标签: 输入样例:
我需要构造一个通用XPath来找到正确的节点,其中的标准是日期和时间。例如查找“ 5 May”,“ 12:17:44”的节点 XML具有日期和时间标签。 不方便地,日期标签仅在当天的第一次出现时填充。
我正在尝试获取xPath几个月内两个日期之间的差异。 几天之内我就没问题了(1127) days-from-duration(xs:date('2012-06-17')-xs:date('2009-0
我试图选择一个包含一段文本的元素,但是我不想选择包含该文本加上其他文本的元素。我通常会使用text()='abc def',但是这个特定元素在前后都包含空格。 这是一个示例片段:
亲爱的,您能帮我用这个XPATH吗?可以说我有以下HTML代码 text value1 value2 text 我需要构建一
我正在尝试提取带有排除项的 xpath,但无法执行此操作。 (//div[@class='row site country-names']/following-sibling::div)[1]/di
response.xpath('//*[@id="blah"]//text()') 假设我的html是 This is a simple text foo and this is after tag.
除了那些具有"//ul/li[not(@*)][count(*)=0]"父项的人以外,我需要全部接受。我已经尝试过,但是不幸的是它不起作用。 有谁知道,我该怎么处理? 提前致谢。 最佳答案 我认为您需
我使用XPath的问题是,每当我使用“子字符串”功能时,我只会得到一个匹配项,而我想全部获得它们。 另一个问题是,每当我使用“子字符串”和运算符的组合时它只是行不通(没有匹配项)。 例如:http:/
我正在尝试通过其位置和属性获取项目,但不知道如何做。 我要实现的是将这一点统一起来: Xpath("//h4/a[contains(@href,'#vuln_')]") 还有这个: Xpath
我有一个xpath如下: .//*[text()='Name:']/../child::select | .//*[text()='Name:']/../child::span 但是对我来说,它既不紧
我拼命试图在xpath中组合几个过滤器。假设我的数据如下所示: DELETE 1 This is my title my sh
我想在已经通过 xpath 设置的其他元素中使用 xpath 来指示元素的位置。 下面的一个已经通过 xpath 设置(我没有改变) //Base_Code
是否可以使用xpath直接在括号内抓取信息?还是以后再用正则表达式过滤? HTML如下所示: Product name (UN1QU3 C0D3) 使用以下Xpath表达式,我可以在此中获取所有内容:
我试图使用一个XPath表达式来选择一个节点,该节点的子节点与文档中的另一个节点匹配。 匹配将意味着该节点的所有属性都相同。因此,如果将一个节点与多个属性进行比较,则无法进行单独的属性比较。 作为示例
我想在 XPath 表达式中使用 Iverson 括号(即映射 true => 1,false => 0)。 示例:而不是书写 someNumber+(if(elem1/elem2[@attr='12
是否可以以类似方式选择节点? './tr[position() in (1, 3, 7)]' 我只找到以下解决方案: './tr[position() = 1 or position() = 3 or
我是一名优秀的程序员,十分优秀!