gpt4 book ai didi

java - 无法抓取我正在寻找的数据?

转载 作者:行者123 更新时间:2023-11-30 10:40:23 26 4
gpt4 key购买 nike

我正在尝试从 URL 中抓取所附图片中表格中的价格和日期:**** http://www.airfrance.fr/vols/paris+tunis

我成功地抓取了信息,但不是我正在寻找的方式(日期 + 价格)。我用了这几行代码

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

运行这段代码只给我一些价格和几个日期,但不是下图中显示的所有表格。

enter image description here

你能帮我解决这个学习项目的问题吗,谢谢。

最佳答案

问题是您正在解析的日历不在服务器提供的原始源代码中(右键单击 > 查看源代码)。该表是在浏览器呈现页面时使用 JavaScript 生成的(右键单击 > 检查)。

Jsoup 只能解析源代码。因此,您需要先使用 HtmlUnit 之类的内容加载页面,然后将此呈现的分页传递给 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 €
....

来源:Parsing JavaScript Generated Pages

关于java - 无法抓取我正在寻找的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38923324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com