2015-07-20 81 views
1

我有HTML作为字符串,我想从中提取“post_titles”。这是HTML字符串:如何从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(); 

这也给一个空白的字符串。请帮我选择一下我需要用来提取标题的选择器。

回答

2

您必须在您的请求的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(); 
     } 
+0

这是工作绝对好。现在我明白了问题所在。为了从该页面访问html内容(https://ssblecturate.wordpress.com/wp-login.php),我需要提供登录信息,这就是为什么它返回空字符串。 –

+0

我也想问问在我的代码中是否有我的登录信息(id和密码)是好的做法。用户是否有能力使用这些信息并滥用我的博客网站? –

0

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

此正则表达式的意思是“一切以阶级POST_TITLE之间(不完全,但是是为您的样品)

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

你应该能够得到与内容:

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

注:我猜你的post_title不包含“<”...这确实应该会产生一个XML结构错误

1

更新! 希望它的工作给你的:

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

这是在element.html()提供空指针异常。线。 –

+0

@TDG:谢谢! – CodeMonster

+1

我也试过了这个代码(来自'String',而不是来自文件),它的工作原理。 – TDG

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

仍然给出一个空字符串。 logcat出现错误:无效的Cookie标头。不知道是否必须采取措施。 –

+0

这是一个免费的WordPress博客网站:https://ssblecturate.wordpress.com/。此页面不会一次显示所有帖子(只显示为7,当您向下滚动时会显示更多帖子)。所以我使用不同的地址显示所有帖子:https://ssblecturate.wordpress.com/wp-admin/edit.php?post_type=post –