将xPath转换为JSoup查询

时间:2013-05-02 10:32:31

标签: xpath jsoup

有没有人知道JSoup转换器的xPath?我从Chrome获得以下xPath:

 //*[@id="docs"]/div[1]/h4/a

并希望将其更改为Jsoup查询。该路径包含我想要引用的href。

6 个答案:

答案 0 :(得分:12)

这很容易手动转换。

像这样(未经测试)

document.select("#docs > div:eq(1) > h4 > a").attr("href");

文档:

http://jsoup.org/cookbook/extracting-data/selector-syntax


评论中的相关问题

  

尝试获取第一个结果的href:   cbssports.com/info/search#q=fantasy%20tom%20brady

<强>代码

Elements select = Jsoup.connect("http://solr.cbssports.com/solr/select/?q=fantasy%20tom%20brady")
        .get()
        .select("response > result > doc > str[name=url]");

for (Element element : select) {
    System.out.println(element.html());
}

<强>结果

http://fantasynews.cbssports.com/fantasyfootball/players/playerpage/187741/tom-brady
http://www.cbssports.com/nfl/players/playerpage/187741/tom-brady
http://fantasynews.cbssports.com/fantasycollegefootball/players/playerpage/1825265/brady-lisoski
http://fantasynews.cbssports.com/fantasycollegefootball/players/playerpage/1766777/blake-brady
http://fantasynews.cbssports.com/fantasycollegefootball/players/playerpage/1851211/brady-foltz
http://fantasynews.cbssports.com/fantasycollegefootball/players/playerpage/1860955/brady-earnhardt
http://fantasynews.cbssports.com/fantasycollegefootball/players/playerpage/1673397/brady-amack

Developer Console的截图 - 抓取网址

enter image description here

答案 1 :(得分:12)

我正在使用 Google Chrome版本47.0.2526.73 m(64位),我现在可以直接复制与JSoup兼容的选择器路径

Chrome with Selector option



截屏span.com中元素的复制选择器为
#question > table > tbody > tr:nth-child(1) > td.postcell > div > div.post-text > pre > code > span.com

答案 2 :(得分:1)

我已经测试了以下XPath和Jsoup,它可以工作。

示例1:

[XPath]中

//*[@id="docs"]/div[1]/h4/a

[JSoup]

document.select("#docs > div > h4 > a").attr("href");

示例2:

[XPath]中

//*[@id="action-bar-container"]/div/div[2]/a[2]

[JSoup]

document.select("#action-bar-container > div > div:eq(1) > a:eq(1)").attr("href"); 

答案 3 :(得分:1)

以下是使用Xsoup和Jsoup的工作独立代码段:

import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import us.codecraft.xsoup.Xsoup;

public class TestXsoup {
    public static void main(String[] args){

            String html = "<html><div><a href='https://github.com'>github.com</a></div>" +
                    "<table><tr><td>a</td><td>b</td></tr></table></html>";

            Document document = Jsoup.parse(html);

            List<String> filasFiltradas = Xsoup.compile("//tr/td/text()").evaluate(document).list();
            System.out.println(filasFiltradas);

    }
}

输出:

[a, b]

图书馆包括:

xsoup-0.3.1.jar jsoup-1.103.jar

答案 4 :(得分:0)

取决于你想要的。

Document doc = JSoup.parse(googleURL);
doc.select("cite") //to get all the cite elements in the page

doc.select("li > cite") //to get all the <cites>'s that only exist under the <li>'s

doc.select("li.g cite") //to only get the <cite> tags under <li class=g> tags


public static void main(String[] args) throws IOException {
    String html = getHTML();
    Document doc = Jsoup.parse(html);
    Elements elems = doc.select("li.g > cite");
    for(Element elem: elems){
        System.out.println(elem.toString());
    }
}

答案 5 :(得分:0)

您不一定需要将Xpath转换为JSoup特定选择器。

相反,您可以使用基于JSoup的XSoup并支持Xpath。

https://github.com/code4craft/xsoup

以下是使用文档中的XSoup的示例。

@Test
public void testSelect() {

    String html = "<html><div><a href='https://github.com'>github.com</a></div>" +
            "<table><tr><td>a</td><td>b</td></tr></table></html>";

    Document document = Jsoup.parse(html);

    String result = Xsoup.compile("//a/@href").evaluate(document).get();
    Assert.assertEquals("https://github.com", result);

    List<String> list = Xsoup.compile("//tr/td/text()").evaluate(document).list();
    Assert.assertEquals("a", list.get(0));
    Assert.assertEquals("b", list.get(1));
}