我正在使用jsoup创建一个类,它将执行以下操作:
下面是我正在尝试做的粗略工作,而不是非常粗糙,因为我一直在尝试很多不同的事情
public class ParsePage {
private String path;
Connection.Response response = null;
private ParsePage(String langLocale){
try {
response = Jsoup.connect(path)
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
.timeout(10000)
.execute();
} catch (IOException e) {
System.out.println("io - "+e);
}
}
public int getSitemapStatus(){
int statusCode = response.statusCode();
return statusCode;
}
public ArrayList<String> getUrls(){
ArrayList<String> urls = new ArrayList<String>();
}
}
正如您所看到的,我可以获取页面状态,但是使用构造函数中已经打开的连接我不知道如何解析文档,我尝试使用:
Document doc = connection.get();
但那是不行的。有什么建议?或者更好的方法来解决这个问题?
答案 0 :(得分:13)
正如Connection.Response类型的JSoup文档中所述,有一个parse()
方法将响应的主体解析为Document
并返回它。
当你拥有它时,你可以随心所欲地做任何事情。
例如,请参阅getUrls()
public class ParsePage {
private String path;
Connection.Response response = null;
private ParsePage(String langLocale){
try {
response = Jsoup.connect(path)
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
.timeout(10000)
.execute();
} catch (IOException e) {
System.out.println("io - "+e);
}
}
public int getSitemapStatus() {
int statusCode = response.statusCode();
return statusCode;
}
public ArrayList<String> getUrls() {
ArrayList<String> urls = new ArrayList<String>();
Document doc = response.parse();
// do whatever you want, for example retrieving the <url> from the sitemap
for (Element url : doc.select("url")) {
urls.add(url.select("loc").text());
}
return urls;
}
}
答案 1 :(得分:6)
如果您不需要登录,请使用:
Document doc = Jsoup.connect("url").get();
如果您需要登录我建议使用:
Response res = Jsoup.connect("url")
.data("loginField", "yourUser", "passwordField", "yourPassword")
.method(Method.POST)
.execute();
Document doc = res.parse();
//If you need to keep logged in to the page, use
Map<String, String> cookies = res.cookies;
//And by every consequent connection, you'll need to use
Document pageWhenAlreadyLoggedIn = Jsoup.connect("url").cookies(cookies).get();
在您使用网址时我可能会尝试
Elements elems = doc.select(a[href]);
for (Element elem : elems) {
String link = elem.attr("href");
}
这就是它。保持良好的工作
答案 2 :(得分:2)
您应该能够在响应对象上调用parse()。
Document doc = response.parse();
答案 3 :(得分:2)
看来你的情况就像你想与jsoup建立连接然后检查状态代码然后根据你要解析的状态代码或者你想要做的任何事情。
首先你需要检查URL的状态代码,而不是创建连接。
Response response = Jsoup.connect("Your Url ").followRedirects(false).execute();
System.out.println(response.statusCode() + " : " + response.url());
response.statusCode()
将返回状态代码
之后您可以创建连接
if (200 == response.statusCode()) {
doc = Jsoup.connect(" Your URL").get();
Elements elements = doc.select("href");
/* what ever you want to do*/
}
您的课程将如下所示
package com.demo.soup.core;
import java.io.IOException;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
/**
* The Class DemoConnectionWithJsoup.
*
* @author Ankit Sood Apr 21, 2017
*/
public class DemoConnectionWithJsoup {
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
Response response;
try {
response = Jsoup.connect("Your URL ").followRedirects(false).execute();
/* response.statusCode() will return you the status code */
if (200 == response.statusCode()) {
Document doc = Jsoup.connect("Your URL").get();
/* what ever you want to do */
}
} catch (IOException e) {
e.printStackTrace();
}
}
}