Java Jsoup 正在解析站点但未获取所需的元素
Java Jsoup parsing a site but not getting the needed elements
https://goworkabit.com/tooampsud
如何解析该元素的信息?
我试过这个:
try {
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
Connection connection; = Jsoup.connect("https://goworkabit.com/tooampsud").userAgent(USER_AGENT);
Document doc; = connection.get();
Elements jobs = doc.select("c-workbites-list__workbites-title");
for (int i = 0; i < jobs.size(); i++) {
System.out.println(jobs.get(i).text());
}
}
由于 table 中的数据是使用 javascript 加载的,当您仅使用 Jsoup 解析页面时,它不会包含来自 table 的任何数据。
一种选择是使用任何页面渲染工具(如 Selenium+web 浏览器)渲染页面,然后使用 Jsoup
解析页面
您可以尝试类似下面的代码。这应该为您提供进一步解析所需的数据。请注意,这种方法非常慢,因为它需要物理浏览器来加载数据。
WebDriver driver = new ChromeDriver();
try {
driver.get("https://goworkabit.com/tooampsud");
Document doc = Jsoup.parse(driver.getPageSource());
Elements jobs = doc.select("tr.c-workbites-table__workbites-row");
for (Element job : jobs) {
System.out.print(job.select("td>a.c-workbites-table__workbites-title").text() +" , ");
System.out.println(job.select("td.text-right").text());
}
} catch(Exception e){
e.printStackTrace();
} finally {
driver.quit();
}
System.setProperty("webdriver.chrome.driver","C:\chromedriver.exe");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://goworkabit.com/tooampsud");
Document doc = Jsoup.parse(driver.getPageSource());
Elements jobs = doc.select(".c-workbites-list__workbites-title");
for (Element job : jobs) {
if (!(job.toString()).contains("filled")) {
System.out.println(job.text());
}
}
} catch(Exception e){
e.printStackTrace();
} finally {
driver.quit();
}
这对我有用,谢谢 Krishna!
https://goworkabit.com/tooampsud
如何解析该元素的信息? 我试过这个:
try {
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
Connection connection; = Jsoup.connect("https://goworkabit.com/tooampsud").userAgent(USER_AGENT);
Document doc; = connection.get();
Elements jobs = doc.select("c-workbites-list__workbites-title");
for (int i = 0; i < jobs.size(); i++) {
System.out.println(jobs.get(i).text());
}
}
由于 table 中的数据是使用 javascript 加载的,当您仅使用 Jsoup 解析页面时,它不会包含来自 table 的任何数据。 一种选择是使用任何页面渲染工具(如 Selenium+web 浏览器)渲染页面,然后使用 Jsoup
解析页面您可以尝试类似下面的代码。这应该为您提供进一步解析所需的数据。请注意,这种方法非常慢,因为它需要物理浏览器来加载数据。
WebDriver driver = new ChromeDriver();
try {
driver.get("https://goworkabit.com/tooampsud");
Document doc = Jsoup.parse(driver.getPageSource());
Elements jobs = doc.select("tr.c-workbites-table__workbites-row");
for (Element job : jobs) {
System.out.print(job.select("td>a.c-workbites-table__workbites-title").text() +" , ");
System.out.println(job.select("td.text-right").text());
}
} catch(Exception e){
e.printStackTrace();
} finally {
driver.quit();
}
System.setProperty("webdriver.chrome.driver","C:\chromedriver.exe");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://goworkabit.com/tooampsud");
Document doc = Jsoup.parse(driver.getPageSource());
Elements jobs = doc.select(".c-workbites-list__workbites-title");
for (Element job : jobs) {
if (!(job.toString()).contains("filled")) {
System.out.println(job.text());
}
}
} catch(Exception e){
e.printStackTrace();
} finally {
driver.quit();
}
这对我有用,谢谢 Krishna!