Reputation: 351
I have HTML as string and i want to extract just "post_titles" from it. this is the HTML string:
<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>
This has the post title as "Single parenting" which is what i want to extract. This is what i am using :
Elements link = doc.select("div[class=post_title]");
String title = link.text();
But this is giving a blank string. I also tried:
Elements link = doc.select("div[id=inline_49]").select("div[class=post_title]");
String title = link.text();
This is also giving a blank string. Please help me what selector exactly I need to use to extract the title.
Upvotes: 1
Views: 2399
Reputation: 1372
You must include a cookie in your request. Check this Java code:
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();
}
Upvotes: 2
Reputation: 2740
Try this, but make sure your HTML text is well formatted in the String :
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());
}
}
Upvotes: 2
Reputation: 300
Updated ! Hope It works for you :
//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();
}
Upvotes: 1
Reputation: 1701
If you have it in a String, you can try with regExp
.
This regex means "everything between with class post_title (not exactly but yes for your sample).
String exp = "<div class=\"post_title\">([^<]*)</div>"
You should be able to get the content with:
String post_title = Pattern.compile(exp).matcher(yourString).group(1);
NOTE: I guess your post_title does not contain "<"... This should indeed generate an XML structure error.
Upvotes: 0