Jsoup.select 未选择 CSS 路径但 chrome 选择了它
Jsoup.select not selecting CSS path but chrome selects it
我正在尝试 select www.zoho.com 中的某些段落使用以下 css 路径:
- html > body > div:nth-of-type(2) > div:nth-of-type(3) > section > div > div > div > div
- div.zh-banner-wrap > div.content-wrap.animated:first-child
它在 Chrome 检查元素 css 路径搜索
中工作正常
但是当我在 jsoup 中尝试这个时,它不起作用。
Java代码:
Document doc = Jsoup.connect("https://www.zoho.com").get();
Element el = doc.selectFirst("html > body > div:nth-of-type(2) > div:nth-of-type(3) > section > div > div > div > div");
if(el != null) {
System.out.println(el.text());
}
试试这个:
Document doc = Jsoup.connect("https://www.zoho.com").get();
Element el = doc.selectFirst("div:nth-of-type(3) > section > div > div > div > div");
if(el != null) {
System.out.println(el.text());
}
您可以找到更多信息here
我不确定您的情况下预期的输出是什么。
当我在 chrome 控制台
中使用你的选择器时
document.querySelector("html > body > div:nth-of-type(2) > div:nth-of-type(3) > section > div > div > div")
我得到了空值。与 Jsoup 相同。
这是因为
document.querySelector("html > body > div:nth-of-type(2)")
是
<div class="ztopstrip-container"></div>
如果您的情况输出不同,请检查是否有通过 JavaScript 动态加载的内容。
将 System.out.println(doc.html())
的结果与网络浏览器的源代码进行比较。
我正在尝试 select www.zoho.com 中的某些段落使用以下 css 路径:
- html > body > div:nth-of-type(2) > div:nth-of-type(3) > section > div > div > div > div
- div.zh-banner-wrap > div.content-wrap.animated:first-child
它在 Chrome 检查元素 css 路径搜索
中工作正常但是当我在 jsoup 中尝试这个时,它不起作用。
Java代码:
Document doc = Jsoup.connect("https://www.zoho.com").get();
Element el = doc.selectFirst("html > body > div:nth-of-type(2) > div:nth-of-type(3) > section > div > div > div > div");
if(el != null) {
System.out.println(el.text());
}
试试这个:
Document doc = Jsoup.connect("https://www.zoho.com").get();
Element el = doc.selectFirst("div:nth-of-type(3) > section > div > div > div > div");
if(el != null) {
System.out.println(el.text());
}
您可以找到更多信息here
我不确定您的情况下预期的输出是什么。 当我在 chrome 控制台
中使用你的选择器时document.querySelector("html > body > div:nth-of-type(2) > div:nth-of-type(3) > section > div > div > div")
我得到了空值。与 Jsoup 相同。
这是因为
document.querySelector("html > body > div:nth-of-type(2)")
是
<div class="ztopstrip-container"></div>
如果您的情况输出不同,请检查是否有通过 JavaScript 动态加载的内容。
将 System.out.println(doc.html())
的结果与网络浏览器的源代码进行比较。