我可以用什么XPath查询来解决下面的问题.我实际上正在使用nokogiri(在红宝石中),所以理想情况下,答案将采用红宝石nokogiri形式,但除此之外我只能使用XPath.
我正在寻求解析下面的HTML(一个完整的html页面,但为了清晰起见,我只是复制/粘贴相关部分),最后基本上是以下内容:
Phone Number Plan ID
545454545 12345
3434343434 67890
Run Code Online (Sandbox Code Playgroud)
所以在Ruby/nokogiri的上下文中,这可能是一个哈希,例如:
% result = { "545454545" => "12345", "3434343434" => "67890" }
Run Code Online (Sandbox Code Playgroud)
.
.
.
<form method="post">
<div style='line-height:18px;background-color:#FFFFFF;border: 1px #dedede solid;padding:10px;'>
<table width='90%' border=0>
<tr>
<td width='30%'> Plan ID </td>
<td width='70%'> 12345 </td>
</tr>
<tr>
<td> Phone Number </td>
<td> 545454545 </td>
</tr>
.
.
.
</table>
</div>
<br>
.
.
.
<div style='line-height:18px;background-color:#FFFFFF;border: 1px #dedede solid;padding:10px;'>
<table width='90%' border=0>
<tr>
<td width='30%'> Plan ID </td> …Run Code Online (Sandbox Code Playgroud) 我想在bash中解析XML,xpath可以做到.
(要获得带有子元素"em:minVersion = 2.1"的"Description"元素)这样的查询效果很好:
xpath install.rdf /RDF/Description/em:targetApplication/Description[em:minVersion=2.1]
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
xpath install.rdf /RDF/Description/em:targetApplication/Description[em:id={ec8030f7-c20a-464f-9b0e-13a3a9e97384}]
Run Code Online (Sandbox Code Playgroud)
输出如下:
Query:
/RDF/Description/em:targetApplication/Description[em:id={{ec8030...
.......................................................^^^
Invalid query somewhere around here (I think)
Run Code Online (Sandbox Code Playgroud)
我认为这是因为大括号"{}"需要转义,所以我尝试了'{{..}}','{...}'......它们都不起作用.
我根本不熟悉xpath或perl ......
我在JMeter中的XML响应中有以下数据:
<details>
<srNo>1</srNo>
<key>123</key>
<Name>Inspector</piName>
<age>89</age>
<country>India</country>
</details>
....................................
...................................
<details>
<srNo>1</srNo>
<key>123</key>
<Name>Inspector</piName>
<age>89</age>
<country>America</country>
</details>
Run Code Online (Sandbox Code Playgroud)
假设我有多个这样的数据,来自XML文件的响应.我想读取"关键"的值.例如.1我必须读"1"并存储在变量中.对于1这样的响应我在XPath提取器中读取它并获得正确的值,但现在我必须遍历它以获得变量中指定数量的键值.假设我想要1000个这样的密钥,那么我必须循环到1000次以获得变量中的所有值.
在变量中获取该值后,我必须在另一个Sampler中使用该值,例如:$ {key1}
任何人都可以提供一个使用java中的xpath从xml文件中提取所有元素及其属性和值的示例吗?
谢谢
我正在为一个有点慢的网站编写一个页面抓取工具,但是我有很多信息要用于小部件目的(经过他们的许可).目前到目前为止,我需要粗略4-5 minutes地执行和解析所有~150 pages我刮擦.它将是一个crontab'd事件,并且在生成时使用临时表,然后在完成后复制到"实时"表,这样就可以从客户端站点无缝过渡,但是你能看到加速的方法吗?我的代码,可能吗?
//mysql connection stuff here
function dnl2array($domnodelist) {
$return = array();
$nb = $domnodelist->length;
for ($i = 0; $i < $nb; ++$i) {
$return['pt'][] = utf8_decode(trim($domnodelist->item($i)->nodeValue));
$return['html'][] = utf8_decode(trim(get_inner_html($domnodelist->item($i))));
}
return $return;
}
function get_inner_html( $node ) {
$innerHTML= '';
$children = $node->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
return $innerHTML;
}
// NEW curl instead of file_get_contents()
$c = curl_init($url);
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_USERAGENT, getUserAgent()); …Run Code Online (Sandbox Code Playgroud) 如何仅选择父跨度webelement而不是孩子?
列表summaryLinks = summary.findElements(By.xpath(""));
我的HTML代码:
<li>
<div class="mod-indent mod-indent-2">
<a href="http://mysite.com">
<img class="activityicon" alt="URL" src="http://mysite.com">
<span class="instancename">LinkText <span class="accesshide "> URL</span></span>
</a>
</div>
</li>
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的xpath但是没有用
.//div [@ class ='mod-indent mod-indent-2']/a/span [@ class ='instancename']
.//div [@class='mod-indent mod-indent-2']/a/span [1]
.//div [@ class ='mod-indent mod-indent-2']/a/span /.
.//div [@class='mod-indent mod-indent-2']/a/span [position()= 1]
.//div [@class='mod-indent mod-indent-2']/a/span/span [position()= 1]
我试图从中提取一些文本和链接instapaper.com.所以我使用以下代码完成工作:
>>> import lxml.html as lh
>>> doc = lh.parse("http://www.instapaper.com/u/folder/1227370/programming")
>>> text = doc.xpath(".//*[@id='bookmark_list']/*/div[3]/a/text()")
>>> len(text)
0
>>> text
[]
Run Code Online (Sandbox Code Playgroud)
如您所见,它返回一个空列表,这意味着它无法找到与上述xpath匹配的任何文本.
现在,当我xpath expr在firebug/firepath中使用上述内容时,它可以正常工作.

您可以在上面的图像中看到它显示40 matching nodes.
所以,我的问题是为什么上面的xpath表达式不能用于python/lxml.
正如所请求的Instapaper页面源
给出以下XML文件:
<a m="1">
<b n="1" o="2">
<c p="3">3</c>
<d/>
</b>
<b n="1" o="2">
<c p="3">3</c>
<d q="3">
<e r="2">2</e>
</d>
<f s="1"/>
</b>
</a>
Run Code Online (Sandbox Code Playgroud)
我如何找到以下表达式:
1. count(/*/*/*) = 5
2. count (/*//*) = 6
3. count (/*/*//@*) = 4
Run Code Online (Sandbox Code Playgroud)
我用Java中的那些xpath表达式运行xml文件,但我不明白为什么答案是5,6,4.
有人可以解释我怎么能计算出上述公式(不使用java代码),而是要了解这些命令的实际概念/*/*/*,并/*//*和/*/*//@*?
非常感激
我试图使用XPath访问Dom的特定元素
这是一个例子
<table>
<tbody>
<tr>
<td>
<b>1</b> <a href="http://www.url.html">data</a><br>
<b>2</b> <a href="http://www.url.html">data</a><br>
<b>3</b> <a href="http://www.url.html">data</a><br>
</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我想以"table td"为目标,所以我在Xpath中的查询是这样的
$finder->query('//table/td');
Run Code Online (Sandbox Code Playgroud)
只有这不会返回td作为子子,并且将使用直接访问
$finder->query('//tr/td');
Run Code Online (Sandbox Code Playgroud)
是否有更好的方法来编写查询,这将允许我使用像第一个例子忽略中间元素并返回TD的东西?
用于计算xml缩进深度的最简单的xpath 2.0是什么?我的变体还不聪明:
<xsl:param name="maxdepth" select="number(substring(concat(
'16'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'15'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'14'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'13'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'12'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'11'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'10'[count(current()/*/*/*/*/*/*/*/*/*/*)>0],
'09'[count(current()/*/*/*/*/*/*/*/*/*)>0],
'08'[count(current()/*/*/*/*/*/*/*/*)>0],
'07'[count(current()/*/*/*/*/*/*/*)>0],
'06'[count(current()/*/*/*/*/*/*)>0],
'05'[count(current()/*/*/*/*/*)>0],
'04'[count(current()/*/*/*/*)>0],
'03'[count(current()/*/*/*)>0],
'02'[count(current()/*/*)>0],
'01'[count(current()/*)>0])
,1,2)
)"/>
Run Code Online (Sandbox Code Playgroud)