Crawl Web page encountered 500 error: User Agent

Source: Internet
Author: User

When you write a spider program, you will find a lot of web pages will encounter 500 errors, this problem I first met, I was so tortured, finally solved, so simple.
When using
URL url = new URL ("http://www.google.cn/search?hl=zh-CN&newwindow=1&q= Beijing &start=0&sa=n");
Url.openstream () found 403 access denied error, is due to Google's Java program restrictions, the lack of agents.

Workaround:
URL url = new URL ("http://www.google.cn/search?hl=zh-CN&newwindow=1&q= Beijing &start=0&sa=n");
URLConnection con = url.openconnection ();
Con.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 5.0; Windows XP; Digext) ");
InputStream in = Con.getinputstream ();

private static Document Geturlcontent () throws Malformedurlexception, IOException, unsupportedencodingexception {
        Document doc = Jsoup.connect ("http://www.tudou.com/programs/view/pVploWOtCQM/")
          . Data ("Query", "Java")
          . UserAgent ("Mozilla").
          Cookie ("auth", "token").
          timeout (+)
          . Post ();
        return doc;
    }


In the writing crawler often encountered some pages to download the problem, if you put the request to simulate the same as the browser, it is sure to be able to get the page correctly, but often many parameters are not used, the real impact on a few key parameters. Here is a special summary of their own experience. 
key parameters to focus on are:
 1. URL: This is primarily a URL encoding can cause problems, the URL contains the Chinese language may appear when the
 2. User-agent: Big websites are usually optimized for different browsers, so there's a difference
 . 3. Cookies: Some websites use cookie information, such as some websites that record session information in a cookie
 4. Refer: Some websites check 5 of refer pages in order to prevent cross-site attacks
 . Accept-charset and accept-encoding: Individual websites will have special handling of the received encoding
 6. SessionID: This is sometimes set in parameters, some sites use this value, and of course other parameters may be useful. Above are the experience of experience
 
, hope to be helpful to everyone.
Original address: http://www.zhizhihu.com/html/y2011/3058.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.