将xPath转换为JSoup查询

将xPath转换为JSoup查询,xpath,jsoup,Xpath,Jsoup,有人知道xPath到JSoup转换器吗?我从Chrome获得以下xPath: //*[@id="docs"]/div[1]/h4/a 并希望将其更改为Jsoup查询。路径包含我试图引用的href。这非常容易手动转换 像这样的东西没有经过测试 document.select("#docs > div:eq(1) > h4 > a").attr("href"); 文件: 评论中的相关问题 尝试在此处获取第一个结果的href: cbsports.com/info/searchq

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

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

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

这非常容易手动转换

像这样的东西没有经过测试

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

评论中的相关问题 尝试在此处获取第一个结果的href: cbsports.com/info/searchq=fantasy%20tom%20brady

代码

结果

开发者控制台截图-抓取URL


这取决于你想要什么

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());
    }
}

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

例1:

[XPath]

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

document.selectdocs>div>h4>a.attrref; 例2:

[XPath]

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

document.selectaction-bar-container>div>div:eq1>a:eq1.attrref; 我正在使用Google Chrome 47.0.2526.73 m 64位版本,现在我可以直接复制与JSoup兼容的选择器路径

在屏幕截图span.com中复制的元素选择器为
问题>表格>tbody>tr:nth-child1>td.postcell>div>div.post-text>pre>code>span.com

以下是使用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);

    }
}
图书馆包括:

xsoup-0.3.1.jar
jsoup-1.103.jar

您不一定需要将Xpath转换为特定于jsoup的选择器

相反,您可以使用基于JSoup并支持Xpath的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));
}

不起作用,但可能不是你回答的错。在这里尝试获取第一个结果的href:你是说这个?这个页面是通过AJAX增量加载的,所以您需要像Jsoup中的AJAX一样直接访问这些数据。非常感谢!在通过Jsoup加载AJAX资源方面有什么好的资源吗?我不知道,可能很多,我正在使用Google Chrome。你能解释一下你是如何从我的评论中得到你使用的url的吗?我正在解析来自这个站点的另一个页面,但也遇到了问题。我在一个表上创建了选择器路径,它返回的只是元素上的id标记。你能检查一下吗?很好,这在一定程度上提高了性能。
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]
@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));
}