【问题标题】:How to Extract Content From HTML如何从 HTML 中提取内容
【发布时间】:2015-07-20 15:44:04
【问题描述】:

我有 HTML 作为字符串,我想从中提取“post_titles”。这是 HTML 字符串:

<div class="hidden" id="inline_49">
<div class="post_title">Single parenting</div>
<div class="post_name">single-parenting</div>
<div class="post_author">90307285</div>
<div class="comment_status">open</div>
<div class="ping_status">open</div>
<div class="_status">publish</div>
<div class="jj">20</div>
<div class="mm">07</div>
<div class="aa">2015</div>
<div class="hh">00</div>
<div class="mn">52</div>
<div class="ss">33</div>

这篇文章的标题为“单亲教育”,这是我要提取的内容。这就是我正在使用的:

Elements link = doc.select("div[class=post_title]");
String title = link.text();

但这给出了一个空白字符串。我也试过了:

Elements link = doc.select("div[id=inline_49]").select("div[class=post_title]");
String title = link.text();

这也给出了一个空白字符串。请帮助我确切需要使用什么选择器来提取标题。

【问题讨论】:

    标签: android html jsoup selector


    【解决方案1】:

    您必须在请求中包含 cookie。 检查此 Java 代码:

    try {
    
                String url = "https://ssblecturate.wordpress.com/wp-login.php";
    
                Connection.Response response = Jsoup.connect(url)
                        .data("log", "your_login_here") // your wordpress login
                        .data("pwd", "your_password_here") // your wordpress password
                        .data("rememberme", "forever")
                        .data("wp-submit", "Log In")
                        .method(Connection.Method.POST)
                        .followRedirects(true)
                        .execute();
    
                Document document = Jsoup.connect("https://ssblecturate.wordpress.com/wp-admin/edit.php")
                        .cookies(response.cookies())
                        .get();
    
                Element titleElement= document.select("div[class=post_title]").first();
                System.out.println(titleElement.text());
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    【讨论】:

    • 这绝对没问题。现在我明白了问题所在。为了从该页面 (ssblecturate.wordpress.com/wp-login.php) 访问 html 内容,我需要提供登录信息,这就是它返回空字符串的原因。
    • 我还想问一下在我的代码中包含我的登录信息(ID 和密码)是否是一种好习惯。用户是否能够使用该信息并滥用我的博客网站?
    【解决方案2】:

    试试这个,但要确保你的 HTML 文本在字符串中格式正确:

    String html = "<div class=\"hidden\" id=\"inline_49\">" +
                "<div class=\"post_title\">Single parenting</div>" +
                "<div class=\"post_name\">single-parenting</div>" +
                "<div class=\"post_author\">90307285</div>";
    
    Document document = Jsoup.parse(html);
    Elements divElements = document.select("div");
    for(Element div : divElements) {
        if(div.attr("class").equals("post_title")) {
           System.out.println(div.ownText());
        }
    }
    

    【讨论】:

    • 仍然给出一个空字符串。 logcat 给出错误:无效的 cookie 标头。不知道是不是跟它有关系。
    • 这是一个免费的 wordpress 博客网站:ssblecturate.wordpress.com。此页面不会一次显示所有帖子(仅显示 7 个,当您向下滚动时,更多帖子变得可见)。所以我使用了一个不同的地址来显示所有帖子:ssblecturate.wordpress.com/wp-admin/edit.php?post_type=post
    【解决方案3】:

    更新了! 希望它对你有用:

    //Get div tag with class name is 'post_title'
    
    Document doc;
        try {
            File input = new File("D:\\JAVA\\J2EE\\Bin\\Bin\\Project\\xml\\src\\demo\\index.html");
            doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
            //Get div tag with class name is 'post_title'
            Element element = doc.select("div.post_title").first();
            System.out.println(element.html());
        } catch (Exception e) {
            e.printStackTrace();
        }
    

    【讨论】:

    • 这在 element.html() 处给出了空指针异常;行。
    • 我也试过这段代码(来自String,而不是来自文件),它可以工作。
    【解决方案4】:

    如果你有一个字符串,你可以试试regExp

    此正则表达式表示“与 post_title 类之间的所有内容(不完全正确,但对于您的示例来说是的)。

    String exp = "<div class=\"post_title\">([^<]*)</div>"
    

    您应该能够通过以下方式获取内容:

    String post_title = Pattern.compile(exp).matcher(yourString).group(1);
    

    注意:我猜你的 post_title 不包含“

    【讨论】:

      猜你喜欢
      • 2016-09-09
      • 2012-12-10
      • 2013-11-06
      • 2021-09-09
      • 2012-09-03
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多