如何从 HTML 中提取内容
How to Extract Content From HTML
我有 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>
它的 post 标题为 "Single parenting",这是我要提取的内容。这就是我正在使用的:
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();
这也给出了一个空字符串。请帮助我究竟需要使用什么选择器来提取标题。
如果你有它在一个字符串中,你可以尝试 regExp
。
这个正则表达式的意思是“class post_title 之间的所有内容(不完全是,但对于您的样本来说是)。
String exp = "<div class=\"post_title\">([^<]*)</div>"
您应该能够通过以下方式获取内容:
String post_title = Pattern.compile(exp).matcher(yourString).group(1);
注意:我猜你的 post_title 不包含“<”...这确实会产生 XML 结构错误。
已更新!
希望对你有用:
//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();
}
试试这个,但要确保您的 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());
}
}
您必须在您的请求中包含一个 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();
}
我有 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>
它的 post 标题为 "Single parenting",这是我要提取的内容。这就是我正在使用的:
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();
这也给出了一个空字符串。请帮助我究竟需要使用什么选择器来提取标题。
如果你有它在一个字符串中,你可以尝试 regExp
。
这个正则表达式的意思是“class post_title 之间的所有内容(不完全是,但对于您的样本来说是)。
String exp = "<div class=\"post_title\">([^<]*)</div>"
您应该能够通过以下方式获取内容:
String post_title = Pattern.compile(exp).matcher(yourString).group(1);
注意:我猜你的 post_title 不包含“<”...这确实会产生 XML 结构错误。
已更新! 希望对你有用:
//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();
}
试试这个,但要确保您的 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());
}
}
您必须在您的请求中包含一个 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();
}