Scrapy setting up different proxies for different websites

Source: Internet
Author: User

In the company project, there are some reptiles need to use the domestic agent, some need to use foreign agents, some do not have agents


I tested three scenarios.


Programme one:

Turn on the agent in settings.py and rewrite the downloader_middlewares in the spider, but the override cannot take effect


Scenario Two:

Let Scrapy switch to a different settings.py file, this manual switch is effective, but I studied for a half day do not know if let the program automatically switch, finally give up


Programme III:

Write directly in middlewares.py and use Request.url to determine and enable the appropriate proxy

Class Proxymiddleware (object): Def process_request (self, request, spider): Url=request.url if                ' Baidu.com ' in url:request.meta[' proxy '] = ' here set the domestic HTTP proxy ' elif ' facebook.com ' in URL: request.meta[' proxy ']= ' here set foreign HTTP proxy ' Else:pass

And finally, this is the solution.

Scrapy different agents for different sites

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.