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

Can't scrape the data that i'm looking for?

我正在尝试从 URL 的所附图​​片中抓取 table 中的价格和日期:**** 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();
        }
    }
}

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

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

问题是您正在解析的日历不在服务器提供的原始源代码中(右键单击 > 查看源代码)。 table 是在浏览器呈现页面时使用 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