我想抓取一个网页,请求类型是post,但是我收到一个错误: java.io.IOException:服务器返回HTTP响应代码:523用于URL:http://
public static String readContentFromPost(String urlStr, String content) {
URL url = null;
HttpURLConnection con = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
con.setInstanceFollowRedirects(true);
con.setRequestProperty("Content-Type", "text/html;charset=utf-8");
con.connect();
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
答案 0 :(得分:0)
错误523没有任何标准含义:http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
所以这是您尝试抓取的服务器的一个支持性错误...尝试联系网络管理员以了解其含义。
523并不意味着无法到达的原点......它只意味着在Cloudflare中: https://support.cloudflare.com/hc/en-us/articles/200171946-Error-523-Origin-is-unreachable
使用Google或维基百科等知名服务器试用您的代码,以了解它是否正常运行。
答案 1 :(得分:0)
要抓取在javascript中解决的webPage,可以使用selenium来模拟浏览器以获取数据。 硒:http://www.seleniumhq.org
首先创建一个maven项目并添加:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
然后下载ChromeDriver: http://chromedriver.storage.googleapis.com/index.html?path=2.14/
并将其放在/ usr / local / bin
目录中最后你可以抓取页面:
public static void testSelenium(String url) {
// System.getProperties().setProperty("webdriver.chrome.driver","/Users/freezhan/IDE/tools/chromedriver");
WebDriver webDriver = new ChromeDriver();
webDriver.get(url);
//WebElement webElement = webDriver.findElement(By.xpath("/html"));
System.out.println(webDriver.getPageSource());
webDriver.close();
}