[英]java.io.IOException: Server returned HTTP response code: 503 for URL: Error
I'm scraping data from a website by getting the HTML code from the website then parsing it in Java. 我通过从网站获取HTML代码然后从Java中解析数据来从网站上抓取数据。
I'm currently using java.net.URL as well as java.net.URLConnection. 我目前正在使用java.net.URL和java.net.URLConnection。 This is the code I use to get the HTML code from a certain website (Found on this website , slightly edited to fit my needs):
这是我用来从某个网站获取HTML代码的代码(在此网站上找到,经过略微修改以满足我的需要):
public static String getURL(String name) throws Exception{
//Set URL
String s = "";
URL url = new URL(name);
URLConnection spoof = url.openConnection();
//Spoof the connection so we look like a web browser
spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
String strLine = "";
//Loop through every line in the source
while ((strLine = in.readLine()) != null){
//Prints each line to the console
s = s + strLine + "\n";
}
return s;
}
When I run it, the HTML code is received correctly for about 100-200 webpages. 当我运行它时,大约100-200个网页可以正确接收HTML代码。 However, before I am done grabbing HTML code, I get a "java.io.IOException: Server returned HTTP response code: 503 for URL" exception.
但是,在抓取HTML代码之前,我得到了“ java.io.IOException:服务器返回的HTTP响应代码:URL 503”异常。 I've researched this topic fully and other questions like this one do not cover the package I am using.
我已经充分研究这个话题,像其他的问题, 这样一个并不包括我现在用的包。
Thanks in advance for the help! 先谢谢您的帮助!
Maybe server have a limits. 服务器可能有限制。 In this case you can try Socket and input/outputStream instead of URLConnection
在这种情况下,您可以尝试使用Socket和input / outputStream代替URLConnection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.