Access denied)

Source: Internet
Author: User

To prepare test data, You have to crawl the content of a website in large quantities. To prevent being blocked, the crawler is specially designed as a single-thread synchronous crawler. When about 30 thousand pages are crawled, the other Party sends back Access denied. After a period of time, start the crawler and the result is still Access denied. At this time, it was naive to understand this idea. At the beginning, we should find other methods to avoid this. This article describes these other methods.

1. Disguise User Agent

The user agent is a field in the HTTP protocol. It is used to describe the terminal information that sends an HTTP request. The server can use this field to know who is visiting the website. Every browser and every regular crawler has its fixed user agent. Therefore, you only need to change this field to these well-known user agents, and you can successfully disguise them. However, it is not recommended to disguise well-known crawlers because these crawlers may have fixed IP addresses, such as Baidu crawlers. In contrast, it is a good idea to disguise the browser's user agent, because the browser can be used by anyone. In other words, there is no fixed IP address. We recommend that you prepare the user agents of several browsers, and then randomly select one of these user agents from each request. Several user agents of IE are as follows:

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

Set the Code as follows (assuming Java + httpclient 4.1.2 is used)

Httpget getmethod = new httpget ("url"); getmethod. setheader ("User-Agent", "user agent content ");

 

2. Log in

Although some websites can be accessed without logon, when it detects an abnormal access volume of an IP address, it will immediately request a login. If the verification code is not included, log on to the console. However, make some preparations before logging on. Check the parameters that must be included in the post login request. My practice is to first use Badboy to record the login process, export the process as a jmeter file, and then use jmeter to view the parameters required for login. After checking, you can log on, as shown below:

Defaulthttpclient httpclient = new defaulthttpclient (); httppost postmethod = new httppost ("http://passport.cnblogs.com/login.aspx "); // pay attention to the parameter list <namevaluepair> nvps = new arraylist <namevaluepair> (); nvps. add (New basicnamevaluepair ("tbusername", "fengyan"); nvps. add (New basicnamevaluepair ("tbpassword", "zero"); nvps. add (New basicnamevaluepair ("btnlogin", "login"); nvps. add (New basicnamevaluepair ("_ eventtarget", ""); nvps. add (New basicnamevaluepair ("_ eventargument", ""); nvps. add (New basicnamevaluepair ("_ viewstate", "/callback"); nvps. add (New basicnamevaluepair ("_ eventvalidation", "/wewbqlwwpqpdqlyj/tests"); nvps. add (New basicnamevaluepair ("returnurl", "http://www.cnblogs.com/"); nvps. add (New basicnamevaluepair ("txtreturnurl", "http://www.cnblogs.com/"); postmethod. setentity (New urlencodedformentity (nvps, HTTP. utf_8); httpresponse response = httpclient.exe cute (postmethod );

Httpclient automatically manages cookies, so you can get or post them directly.

 

3. Use a proxy

If the other party uses the access times of an IP address in a certain period of time to determine the crawler, and then the crawler's IP addresses are all blocked, the above disguise will become invalid. This idea of the other party implies the assumption that the crawler's access traffic must be much larger than that of normal users, so it is enough to make this assumption unfeasible. Now the agent is playing. The so-called proxy is a third party between the user and the Website: the user first sends the request to the proxy, and then the proxy sends it to the server, which looks like the proxy is visiting the website. In this case, the server calculates the access to the proxy. If multiple proxies are used at the same time, the access volume of a single IP address is reduced, so the attack may escape. However, the biggest problem with this method is to find a stable proxy (if you have money to buy a proxy, you can ignore this sentence ). I am currently looking for a free proxy, but most of the items I have found are unavailable, and a few of them are unstable. Ask for a free proxy for sharing.

Assume that N proxies have been found/purchased, How should these proxies be managed? My idea is to create an IP pool similar to the memory pool. The benefits of this operation are ease of management and scalability. When there is only one proxy, its usage is as follows:

Defaulthttpclient httpclient = new defaulthttpclient (); // This proxy does not guarantee that you will survive when you see httphost proxy = new httphost ("u120-227.static.grapesc.cz", 8080); httpclient. getparams (). setparameter (connroutepnames. default_proxy, proxy); // If the proxy needs to be authenticated, add the following statement // httpclient. getcredentialsprovider (). setcredentials (New authscope ("proxy adress", proxy port), // new usernamepasswordcredentials ("username", "password ")); // remember to split the website into the following form: httphost targethost = new httphost ("www.cnblogs.com"); // do not add HTTP before the website name: // httpget = new httpget ("/fengyan/"); httpresponse response = httpclient.exe cute (targethost, httpget );

In addition, for ADSL dialing, you do not need to worry about the blocked IP address, because in general, when you dial again, you will get a different IP address.

4. Reduce Access Frequency

What if I can't find a free and stable proxy? The last trick is to reduce the access frequency. This can achieve the same effect as using a proxy-to prevent the other party from looking at the traffic. Of course, the crawling efficiency will be much lower. In addition, reducing access frequency is just a guiding ideology. Under this idea, we can get a lot of specific practices, such: every time a page is captured, it takes a random second to rest (personal feeling is better than the fixed time); Limit the number of pages crawled every day.

 

5. Summary

The "reason" to prevent being blocked is disguised as a normal user. As long as you follow this "reason", you can always find the specific "method ". Even so, it is hard to know all the details when a normal user accesses a website, for example, for a person who has not been very familiar with crawlers. Therefore, it is necessary to know some anti-crawler "methods and then conduct targeted moves. This article describes anti-crawling methods and recommends "anti-crawling policy analysis for Internet websites ".

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.