9-python Proxyhandler Processor (proxy settings)

Source: Internet
Author: User

Proxyhandler processor (proxy settings)

Using proxy IP, this is the second trick of the crawler/anti-crawler, and is usually the best.

Many websites will detect the number of times an IP is visited (through traffic statistics, system logs, etc.), and if the number of visits is not as normal, it will prohibit access to this IP.

So we can set up some proxy server, change an agent every time, even if the IP is forbidden, can still change the IP to continue crawling.

#_ *_ coding:utf-8 _*_ "Created on July 13, 2018 @author:sss function: Use proxy" "Import urllib.requestimport randomproxy_list = [{" HTTP ":" 124.88.67.81:80 "}, {" http ":" 124.88.67.81:80 "}, {" http ":" 124.88.67.81:80 "}, {" http ":" 124.88.67.81:8 0 "}, {" http ":" 124.88.67.81:80 "}]proxy = Random.choice (proxy_list) #构建两个代理Handler, one with proxy IP, one without proxy ip# httpproxy_ Handler = Urllib.request.ProxyHandler ({' http ': ' 124.193.85.88:8080 '}) Httpproxy_handler = Urllib.request.ProxyHandler (proxy) #每次随机一个代理httpProxy_handler = Urllib.request.ProxyHandler ({}) # Even if five IP has an empty dictionary Proxyswitch = True #定义一个代理开关 # proxyswitch = False #定义一个代理开关 # Use Proxy handler object to create custom opener object # Depending on whether the proxy switch is turned on, Use a different proxy mode if Proxyswitch:opener = Urllib.request.build_opener (httpproxy_handler) Else:opener = Urllib.request.build_ Opener (httpproxy_handler) request = Urllib.request.Request (' http://www.baidu.com/') # 1. If you write this, only use Opener.open () A custom proxy can be used when sending a request, while using Urlopen () is not using a custom proxy response = Opener.open (Request) # 2. If this is the case, the opener is applied to the global, then all, whether opener. open () or urlopen () send requests are all using custom proxies # Urllib.request.install_opener (opener) # respense = Urllib.request.urlopen (Request) Print (Response.read ()) print (' finish! ')

  

    • West Thorn Free Proxy IP
    • Quick Agent Free agent
    • PROXY360 Agent
    • Full Network proxy IP

9-python Proxyhandler Processor (proxy settings)

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.