java - Jsoup 获取部分页面

标签 java web-scraping jsoup

我正在尝试抓取投标网站的内容,但无法获取网站的完整页面。我在 xulrunner 上使用 crowbar 首先获取页面(因为 ajax 以惰性方式加载某些元素),然后从文件中抓取。 但是在 bidrivals 网站的主页上,即使本地文件格式正确,这也会失败。 jSoup 似乎只是在 html 代码的中间以“...”字符结尾。 如果有人以前遇到过这种情况,请帮忙。 以下代码被称为 [ this link ].

File f = new File(projectLocation+logFile+"bidrivalsHome");
    try {
        f.createNewFile();
        log.warn("Trying to fetch mainpage through a console.");
        WinRedirect.redirect(projectLocation+"Curl.exe -s --data \"url="+website+"&delay="+timeDelay+"\" http://127.0.0.1:10000", projectLocation, logFile+"bidrivalsHome");
    } catch (Exception e) {
        e.printStackTrace();
        log.warn("Error in fetching the nameList", e);
    }
    Document doc = new Document("");
    try {
        doc = Jsoup.parse(f, "UTF-8", website);
    } catch (IOException e1) {
        System.out.println("Error while parsing the document.");
        e1.printStackTrace();
        log.warn("Error in parsing homepage", e1);
    }

最佳答案

尝试使用 HtmlUnit呈现页面(包括 JavaScript 和 CSS dom 操作),然后将呈现的 HTML 传递给 jsoup。

// load page using HTML Unit and fire scripts
WebClient webClient = new WebClient();
HtmlPage myPage = webClient.getPage(myURL);

// convert page to generated HTML and convert to document
Document doc = Jsoup.parse(myPage.asXml(), baseURI);

// clean up resources        
webClient.close();


page.html - 源代码

<html>
<head>
    <script src="loadData.js"></script>
</head>
<body onLoad="loadData()">
    <div class="container">
        <table id="data" border="1">
            <tr>
                <th>col1</th>
                <th>col2</th>
            </tr>
        </table>
    </div>
</body>
</html>

loadData.js

    // append rows and cols to table.data in page.html
    function loadData() {
        data = document.getElementById("data");
        for (var row = 0; row < 2; row++) {
            var tr = document.createElement("tr");
            for (var col = 0; col < 2; col++) {
                td = document.createElement("td");
                td.appendChild(document.createTextNode(row + "." + col));
                tr.appendChild(td);
            }
            data.appendChild(tr);
        }
    }

加载到浏览器时的 page.html

| Col1   | Col2   |
| ------ | ------ |
| 0.0    | 0.1    |
| 1.0    | 1.1    |

使用jsoup解析page.html获取col数据

    // load source from file
    Document doc = Jsoup.parse(new File("page.html"), "UTF-8");

    // iterate over row and col
    for (Element row : doc.select("table#data > tbody > tr"))

        for (Element col : row.select("td"))

            // print results
            System.out.println(col.ownText());

输出

(空)

发生了什么?

Jsoup 解析从服务器传送的源代码(或在本例中从文件加载)。它不会调用客户端操作,例如 JavaScript 或 CSS DOM 操作。在此示例中,行和列从不附加到数据表。

如何解析我在浏览器中呈现的页面?

    // load page using HTML Unit and fire scripts
    WebClient webClient = new WebClient();
    HtmlPage myPage = webClient.getPage(new File("page.html").toURI().toURL());

    // convert page to generated HTML and convert to document
    doc = Jsoup.parse(myPage.asXml());

    // iterate row and col
    for (Element row : doc.select("table#data > tbody > tr"))

        for (Element col : row.select("td"))

            // print results
            System.out.println(col.ownText());

    // clean up resources        
    webClient.close();

输出

0.0
0.1
1.0
1.1

关于java - Jsoup 获取部分页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6368045/

相关文章:

java - Play Framework - 新项目中不存在包

java - 在 GUI 中发生异常后我该去哪里?

Java:链表反向

java - Gradle lint 和测试构建错误 - JAXBException 和库中无效的包引用

javascript - 使用 BeautifulSoup 抓取包含 JavaScript 的网页

python - 无法使用请求从网页中获取所有链接

javascript - 使用 Casperjs 的两个标签检索内容

java - 在 jsoup 中使用正则表达式

java - 为什么这个 JSoup 查询仅在省略 <i> 标记时才有效?

java - 连接到瑞典维基百科以提取信息