java.io.IOException: Server returned HTTP response code: 403 for URL: http://的解决办法

来源:互联网 发布:kali linux arp欺骗 编辑:程序博客网 时间:2024/05/16 09:27
Exception in thread "main" org.htmlparser.util.ParserException: Exception getting input stream from http://blog.csdn.net/w1242245(Server returned HTTP response code: 403 for URL: http://blog.csdn.net/w1242245).;

java.io.IOException: Server returned HTTP response code: 403 for URL: http://blog.csdn.net/w1242245


今天在爬虫扒取网页的时候,其中有一个网页我发现不能像往常一样将内容扒下,增加以下内容解决:


原:

    private Parser parser;
    public TestUrl(String htmlAddress) throws ParserException, IOException {  
        parser = new Parser(htmlAddress);  
       parser.setEncoding("gb2312");
   }


修改以后:

    private Parser parser;  
    public TestUrl(String htmlAddress) throws ParserException, IOException {   
        URL url = new URL(htmlAddress);
        HttpURLConnection connection = (HttpURLConnection) url.
            openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        parser = new Parser(connection);  
       parser.setEncoding("gb2312");
   }


成功解决!

0 0
原创粉丝点击