Javascript js2xml中的xpath是否允许我在选择时执行包含之类的操作?
当用下面的javascript代码抓取页面时,我想知道分配给myProp2的值Javascript js2xml中的xpath是否允许我在选择时执行包含之类的操作?,javascript,xpath,web-scraping,scrapy,js2xml,Javascript,Xpath,Web Scraping,Scrapy,Js2xml,当用下面的javascript代码抓取页面时,我想知道分配给myProp2的值 myProp1={col1: 'firstName', col2: 'lastName'}; myProp2='data'; js2xml提供了一个xpath,但它不允许我执行类似于contains的操作,我可以在Scrapy的xpath中执行 我希望能做到: xpath('//assign[contains(., "myProp2")]/right/*') 要获取分配给myProp2但似乎包含的值(我在Scra
myProp1={col1: 'firstName', col2: 'lastName'};
myProp2='data';
js2xml提供了一个xpath,但它不允许我执行类似于contains的操作,我可以在Scrapy的xpath中执行
我希望能做到:
xpath('//assign[contains(., "myProp2")]/right/*')
要获取分配给myProp2但似乎包含的值(我在Scrapy中使用)不可用
我的解决方法是执行两次xpath选择,然后并行迭代它们,并仅在获得标识符匹配后获取目标值:
import js2xml
from StringIO import StringIO
from lxml import etree
f = StringIO(
"""
<html>
<head>
<script type='text/javascript'>
myProp1={col1: 'firstName', col2: 'lastName'};
myProp2='data';
</script>
</head>
<body>
This has test javascript.
</body>
</html>
""")
tree = etree.parse(f)
for script in tree.xpath('//script/text()'):
jstree = js2xml.parse(script)
idtree = jstree.xpath('//assign/left/*')
valtree = jstree.xpath('//assign/right/*')
for ids, vals in zip(idtree, valtree):
id = js2xml.jsonlike.make_dict(ids)
val = js2xml.jsonlike.make_dict(vals)
if id == 'myProp2':
print(val)
我将在很多地方这样做,所以一些像contains那样提供功能的东西将是有用的
它可能就在那里,我只是不明白。在js2xml的xpath中有什么方法可以做到这一点吗
更新:这是一个基本的xpath表达式问题,与js2xml无关
对于阅读本文的任何人来说,如果有类似xpath初学者的问题,我已经了解到xpath测试站点在学习如何编写xpath表达式时非常有帮助。js2xml.parse返回一个表示JavaScript指令的lxml树。
但是分配的标识符在输出XML中不会显示为文本节点,因此通常不能执行包含操作。。。直接在指定节点上,但可以在其某些子属性上
让我们首先看看js2xml提供给您的XML:
>>> s = '''
... myProp1={col1: 'firstName', col2: 'lastName'};
... myProp2='data';'''
>>> import js2xml
>>> jstree = js2xml.parse(s)
>>> print(js2xml.pretty_print(jstree))
<program>
<assign operator="=">
<left>
<identifier name="myProp1"/>
</left>
<right>
<object>
<property name="col1">
<string>firstName</string>
</property>
<property name="col2">
<string>lastName</string>
</property>
</object>
</right>
</assign>
<assign operator="=">
<left>
<identifier name="myProp2"/>
</left>
<right>
<string>data</string>
</right>
</assign>
</program>
parse返回一个表示JavaScript指令的lxml树。
但是分配的标识符在输出XML中不会显示为文本节点,因此通常不能执行包含操作。。。直接在指定节点上,但可以在其某些子属性上
让我们首先看看js2xml提供给您的XML:
>>> s = '''
... myProp1={col1: 'firstName', col2: 'lastName'};
... myProp2='data';'''
>>> import js2xml
>>> jstree = js2xml.parse(s)
>>> print(js2xml.pretty_print(jstree))
<program>
<assign operator="=">
<left>
<identifier name="myProp1"/>
</left>
<right>
<object>
<property name="col1">
<string>firstName</string>
</property>
<property name="col2">
<string>lastName</string>
</property>
</object>
</right>
</assign>
<assign operator="=">
<left>
<identifier name="myProp2"/>
</left>
<right>
<string>data</string>
</right>
</assign>
</program>
保罗对如何使用contains这个问题给出了最好的答案 这里有另一个表达式,虽然它提供了相同的结果,但不使用contains,而是使用谓词,这样更容易看到匹配应该发生在哪里
//assign[left/identifier[@name="myProp1"]]/right/*
保罗对如何使用contains这个问题给出了最好的答案 这里有另一个表达式,虽然它提供了相同的结果,但不使用contains,而是使用谓词,这样更容易看到匹配应该发生在哪里
//assign[left/identifier[@name="myProp1"]]/right/*
嘿,太棒了。这向我展示了方括号的作用,这让我开始阅读xpath谓词。因此,与其包含我可以做的,不如使用这样的谓词://assign[left/identifier[@name=myProp1]]]//right/*Heh,太棒了。这向我展示了方括号的作用,这让我开始阅读xpath谓词。因此,与其包含我可以做的,不如使用这样的谓词://assign[left/identifier[@name=myProp1]]]]/right/*事实上,如果您知道变量名,您可以显式使用。它甚至可以是//assign[left/identifier/@name=myProp1]/right/*事实上,如果您知道变量名,那么您可以显式使用。它甚至可以是//assign[left/identifier/@name=myProp1]/right/*