【问题标题】:Java Jsoup can't select tableJava Jsoup 无法选择表
【发布时间】:2015-04-13 14:19:51
【问题描述】:

我最近开始做一个小项目,所以我可以学习 Jsoup 的基础,但是我在特定网站上选择表格时遇到了一些困难。我正在尝试使用 Jsoup 获取表格但没有成功(见图)http://imgur.com/RC21UBk

我知道我要获取的表格具有 class="meddelande",并且也在具有相同 class="meddelande" 的表单元素内。 网站HTML代码:http://pastebin.com/ufRDhLSy

我正在尝试获取红色标记的区域,您知道该怎么做吗? 提前致谢! :)

我的代码:

public void startMessage(String cookie1) {
    try {
        doc1 = Jsoup.connect("https://nya.boplats.se/minsida/meddelande")
                .timeout(0).cookie("Boplats-Session", cookie1)
                .get();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Elements tables = doc1.select("form.meddelande");
    Elements table = tables.select("table.meddelande");
    System.out.println(table);
}

【问题讨论】:

标签: java web-scraping web-crawler jsoup


【解决方案1】:

在你的代码中

Elements tables = doc1.select("form.meddelande");    
Elements table = tables.select("table.meddelande");

您正在尝试使用class 属性meddelande 访问form,但从您链接的HTML 源meddelandeid,而不是class,所以不是

form.meddelande

你应该使用

form#meddelande
    ^--# means id, dot represents class

所以试试

Elements tables = doc.select("form#meddelande");    
Elements table = doc.select("table.meddelande");

或者更简单的

Elements table = doc.select("form#meddelande table.meddelande");

如果这不起作用,那么负责表格的 HTML 代码可能是由 JavaScript 生成的。在这种情况下,您将无法使用 Jsoup 获得它,但您需要 Selenium web driverHtmlUtil 之类的东西

【讨论】:

    【解决方案2】:

    在您的情况下,最好选择未读和已读的课程。

    import java.io.File;
    import java.io.IOException;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    public class JsoupSO {
    
        public static void main(String args0[]) throws IOException {
            Document doc;
            Elements elements;
    
            doc = Jsoup.parse(new File("path_to_file or use connect for URL"), "UTF-8");
    
            elements = doc.getElementsByClass("unread");
            for (Element element : elements) {
                System.out.println(element);
            }
    
            elements = doc.getElementsByClass("read");
            for (Element element : elements) {
                System.out.println(element);
            }
        }
    
    }
    

    输出:http://pastebin.com/CwG1cL5T

    是的,请阅读他们的食谱http://jsoup.org/

    【讨论】:

    • 没关系,我很高兴我可以帮助别人 ;-)
    【解决方案3】:

    这是一个尝试

    Document doc = Jsoup.connect("http://pastebin.com/raw.php?i=ufRDhLSy").get();
    System.out.println(doc.select("table[class=meddelande]"));
    

    或仅在选择具有特定类的节点时使用较短的语法

    System.out.println(doc.select("table.meddelande"));
    

    JSoup 支持选择器语法。因此,您可以使用它来选择具有特定属性的 DOM 节点 - 在本例中是类属性。

    有关选择器语法中更复杂的选项,请查看此处http://jsoup.org/cookbook/extracting-data/selector-syntax

    【讨论】:

    • 感谢这本书,不知道那本书。您的代码也有帮助! :D 谢谢
    猜你喜欢
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2011-09-02
    • 2016-07-04
    相关资源
    最近更新 更多