打开与Jsoup的连接,获取状态代码并解析文档

我正在使用jsoup创建一个类,它将执行以下操作:

  1. 构造函数打开与url的连接。
  2. 我有一个方法,将检查页面的状态。 即200,404等
  3. 我有一个方法来解析页面并返回一个url列表。#

下面是我正在尝试做的粗略工作,而不是非常粗糙,因为我一直在尝试很多不同的事情

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 getUrls(){ ArrayList urls = new ArrayList(); } } 

正如您所看到的,我可以获取页面状态,但是使用构造函数中已经打开的连接我不知道如何解析文档,我尝试使用:

 Document doc = connection.get(); 

但那是不行的。 有什么建议么? 或者更好的方法来解决这个问题?

正如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 getUrls() { ArrayList urls = new ArrayList(); Document doc = response.parse(); // do whatever you want, for example retrieving the  from the sitemap for (Element url : doc.select("url")) { urls.add(url.select("loc").text()); } return urls; } } 

如果您不需要登录,请使用:

 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 cookies = res.cookies; //And by every consequent connection, you'll need to use Document pageWhenAlreadyLoggedIn = Jsoup.connect("url").cookies(cookies).get(); 

在你的使用中获取url我可能会尝试

 Elements elems = doc.select(a[href]); for (Element elem : elems) { String link = elem.attr("href"); } 

这就是它。保持良好的工作

您应该能够在响应对象上调用parse()。

 Document doc = response.parse(); 

似乎你的情况就像你想与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(); } } }