Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当每个元素的元素值不同时,如何使用xpath提取特定的元素值_Xpath_Scrapy - Fatal编程技术网

当每个元素的元素值不同时,如何使用xpath提取特定的元素值

当每个元素的元素值不同时,如何使用xpath提取特定的元素值,xpath,scrapy,Xpath,Scrapy,如何使用xpath提取ABC,XYZ <div id="desc" class="description"> <span class="category">Name:</span> <span class="category-detail"><a href="/name/">Name</a></span> <br/> <span class="category">

如何使用xpath提取ABC,XYZ

<div id="desc" class="description">

    <span class="category">Name:</span> <span class="category-detail"><a href="/name/">Name</a></span>
    <br/>
    <span class="category">Address:</span> <span class="category-detail">ABC, XYZ</span>
    <br/>
    <span class="category">Room No:</span> <span class="category-detail">20</span>
    <br/>

但是我得到了
[Name,ABC,XYZ,20]
但是我只需要
ABC,XYZ
尝试使用下面的
XPath
来获得所需的输出:

//div[@id="desc"]/span[.="Address:"]/following-sibling::span[1]/text()
改用这个xpath

WebDriver=newfirefoxdriver();
驱动程序。获取(“file:///C:/Users/sv/Desktop/docUpload.html");
List spanList=driver.findelelements(By.xpath(“../div[@id='desc']/span”);
对于(int i=0;i

您可以在元素列表中循环。

您使用的是Scrapy吗?是的,我使用的是Scrapy。您能告诉我为什么在[.=”地址:]中使用句点吗?在这种情况下,它表示
span
的内容。您可以使用
text()=”地址:“
也是。您还可以通过使用css选择器检查以获取更多详细信息任何解决方案?您可以尝试类似于
div#desc span.category detail:nth child(5)
,但它看起来不太可靠。非常感谢您提出了同样的问题:房间号:那么我们如何从中提取href value(xyz.com)是的,Xpath给出了正确的结果。谢谢顺便说一句,你能告诉我在哪里我可以学习这些复杂的XPath类型,通过这些网站。非常感谢
//div[@id="desc"]/span[.="Address:"]/following-sibling::span[1]/text()
//div[@id="desc"]//span[text()='Address:']//following::span[1]/text()
WebDriver driver = new FirefoxDriver();
    driver.get("file:///C:/Users/sv/Desktop/docUpload.html");
    List<WebElement> spanList = driver.findElements(By.xpath(".//div[@id='desc']/span"));
    for (int i =0 ;i < spanList.size();i++){
        String text = spanList.get(i).getText();
        if(text.equals("ABC, XYZ")){
            System.out.println(text);
        }
    }