Scrapy disguise proxy and use of fake_userAgent, scrapyuseragent

Source: Internet
Author: User

Scrapy disguise proxy and use of fake_userAgent, scrapyuseragent

In disguise, browser proxy crawling web pages is not very high for some servers to filter requests. You do not need ip addresses to disguise requests and directly send your browser information to disguise.

Method 1:

1. Add the following content to the setting. py file, which is the header information of Some browsers.

USER_AGENT_LIST = ['zspider/0.9-dev http://feedback.redkolibri.com /',
'Xaldon _ WebSpider/2.0.b1 ',
'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) Speedy Spider (http://www.entireweb.com/about/search_tech/speedy_spider )',
'Mozilla/5.0 (compatible; Speedy Spider; http://www.entireweb.com/about/search_tech/speedy_spider )',
'Speedy Spider (Entireweb; Beta/1.3; http://www.entireweb.com/about/search_tech/speedyspider )',
'Speedy Spider (Entireweb; Beta/1.2; http://www.entireweb.com/about/search_tech/speedyspider )',
'Speedy Spider (Entireweb; Beta/1.1; http://www.entireweb.com/about/search_tech/speedyspider )',
'Speedy Spider (Entireweb; Beta/1.0; http://www.entireweb.com/about/search_tech/speedyspider )',
'Speedy Spider (Beta/1.0; www.entireweb.com )',
'Speedy Spider (http://www.entireweb.com/about/search_tech/speedy_spider )',
'Speedy Spider (http://www.entireweb.com/about/search_tech/speedyspider )',
'Speedy Spider (http://www.entireweb.com )',
'Sosospider + (+ http://help.soso.com/webspider.htm )',
'Sogou spider ',
'Nusearch Spider (www.nusearch.com )',
'Nusearch Spider (compatible; MSIE 4.01; Windows NT )',
'Lmspider (lmspider@scansoft.com )',
'Lmspider lmspider@scansoft.com ',
'Ldspider (http://code.google.com/p/ldspider/wiki/Robots )',
'Iaskspider/2.0 (+ http://iask.com/help/help_index.html )',
'Askspider ',
'Hl _ ftien_spider_v1.1 ',
'Hl _ ftien_spider ',
'Maid (+ http://www.fybersearch.com/fyberspider.php )',
'Fyberspider ',
'Everyfeed-spider/2.0 (http://www.everyfeed.com )',
'Envolk [ITS] spider/1.6 (+ http://www.envolk.com/envolkspider.html )',
'Envolk [ITS] spider/1.6 (http://www.envolk.com/envolkspider.html )',
'Baidider + (+ http://www.baidu.com/search/spider_jp.html )',
'Baidider + (+ http://www.baidu.com/search/spider.htm )',
'Baidider ',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0) AddSugarSpiderBot www.idealobserver.com ',
]
2. Create a MidWare file price under the directory of the spider level and write the HeaderMidWare. py file content
1 # encoding: UTF-8 2 from scrapy. utils. project import get_project_settings 3 import random 4 5 settings = get_project_settings () 6 7 class ProcessHeaderMidware (): 8 "process request add request info" 9 10 def process_request (self, request, spider): 11 "12 randomly obtain the header from the list, and pass it to user_agent for 13" 14 ua = random. choice (settings. get ('user _ AGENT_LIST ') 15 spider.logger.info (msg = 'now entring download midware') 16 if ua: 17 request. headers ['user-agent'] = ua18 # Add desired logging message here.19 spider.logger.info (u'user-Agent is :{}{}'. format (request. headers. get ('user-agent'), request) 20 pass
View Code

3. In the setting. py file, add

DOWNLOADER_MIDDLEWARES = {
'Projectname. MidWare. HeaderMidWare. ProcessHeaderMidware ': 543,
}



Method 2: Use of fake_userAgent
Fake_userAgent is an open-source project on github.
1. Install fake_userAgent
pip install fake-useragent
2.Create a MidWare file under the directory of the spider level and write a user_agent_middlewares.py file with the content
1 #-*-coding: UTF-8-*-2 from fake_useragent import UserAgent 3 4 class RandomUserAgentMiddlware (object ): 5 # Change user-agent 6 def _ init _ (self, crawler): 7 super (RandomUserAgentMiddlware, self ). _ init _ () 8 self. ua = UserAgent () 9 self. ua_type = crawler. settings. get ('random _ UA_TYPE ', 'rand') # Read the value of RANDOM_UA_TYPE from the setting file 10 11 @ classmethod12 def from_crawler (cls, crawler): 13 return cls (crawler) 14 15 def process_request (self, request, spider): ### system electrophoresis function 16 def get_ua (): 17 return getattr (self. ua, self. ua_type) 18 # user_agent_random = get_ua () 19 request. headers. setdefault ('user _ agent', get_ua () 20 pass

3. Add in setting. py

RANDOM_UA_TYPE = 'random '# random chrome

DOWNLOADER_MIDDLEWARES = {

'Projectname. MidWare. user_agent_middlewares.RandomUserAgentMiddlware ': 543,

'Scrapy. downloadermiddlewares. useragent. UserAgentMiddleware ': None,

}

 

The fake_userAgent camouflage proxy is configured. Compared with the first method, you do not need to write a large string of browser headers.

In the https://fake-useragent.herokuapp.com/browsers/0.1.7.

 

Some errors occur when fake_userAgent is enabled for the first time. I think it is caused by caching some content when the project requests the network.

 

Github address: https://github.com/sea1234/fake-useragent



 

DOWNLOADER_MIDDLEWARES = {
'ningxianggonggongziyuan.MidWare.HeaderMidWare.ProcessHeaderMidware': 543,
}

Related Article

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.