Java Can';t刮取i';我在找什么?
我正试图从URL上抓取附件图片表中的价格和日期:** 我成功地获取了信息,但没有找到我想要的方式(日期+价格)。我使用了这些代码行Java Can';t刮取i';我在找什么?,java,web-scraping,web-crawler,jsoup,Java,Web Scraping,Web Crawler,Jsoup,我正试图从URL上抓取附件图片表中的价格和日期:** 我成功地获取了信息,但没有找到我想要的方式(日期+价格)。我使用了这些代码行 import java.io.IOException; import javax.lang.model.element.Element; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; public class Test {
import java.io.IOException;
import javax.lang.model.element.Element;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class Test {
public static void main(String[] args) {
Document doc;
try {
doc = Jsoup.connect("http://www.airfrance.fr/vols/paris+tunis").get();
Elements links = doc.select("div");
for (org.jsoup.nodes.Element e:links) {
System.out.println(e.text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行此代码只会给我一些价格和一些日期,但不是下图所示的所有表格
您能帮我解决我的学习项目的这个问题吗?谢谢。问题是您正在解析的日历不在服务器提供的原始源代码(右键单击>查看源代码)中。该表是在浏览器呈现页面时使用JavaScript生成的(右键单击>检查) Jsoup只能解析源代码。因此,您需要首先使用以下内容加载页面,然后将呈现的页面传递给Jsoup
// load page using HTML Unit and fire scripts
WebClient webClient = new WebClient();
HtmlPage myPage = webClient.getPage("http://www.airfrance.fr/vols/paris+tunis");
// convert page to generated HTML and convert to document
Document doc = Jsoup.parse(myPage.asXml());
// find all of the date/price cells
for(Element cell : doc.select("td.available.daySelection")) {
String cellDate = cell.select(".cellDate").text();
String cellPrice = cell.select(".cellPrice > .day_price").text();
System.out.println(
String.format(
"cellDate=%s cellPrice=%s",
cellDate,
cellPrice));
}
// clean up resources
webClient.close();
控制台
cellDate=1 septembre cellPrice=302 €
cellDate=2 septembre cellPrice=270 €
cellDate=3 septembre cellPrice=270 €
cellDate=4 septembre cellPrice=270 €
cellDate=5 septembre cellPrice=270 €
....
来源:谢谢,您确实遇到了问题,但我在尝试您的代码时,webClient.close()仍然存在问题。无法在此处发布!!!悲哀的是它太长了。我向您发送了一个facebook请求,如果您不介意,请将您的邮件传递给我,我将不胜感激。请打开一个新问题,详细说明您在WebClient.close()中遇到的问题。您应该只发布代码的相关部分,而不是整个源代码。我只是缺少很多依赖项。您是否添加了特定的包?请参阅Html单元答案中的链接。