HTTP request class based on Python third-party requests module

Source: Internet
Author: User

A Third-party library is installed first using the requests module-constructed Downloader requests

PIP Install requests


1
classstrongdownload (object):2 3 def __init__(self):4 #Get Agent IPList .5Self.iplist = ['Find your own way.']#6Self. useragent = ['Find your own way.'] 7 8 defGet (self,url,timeout,proxy=false,num_retries=3):9 " "URL timeout,proxy,num_retriesTen returns the Response object one " " a #Forged UserAgent -UA = Self. Useragent[random.randint (0,len (self). Useragent)-1)] - #UA = Random.choice (self. Useragent) theheaders = {'user-agent': UA} - ifProxy = =False: - Try: - returnRequests.get (url,timeout=timeout,headers=Headers) + exceptException as E: - Printe + #Judging Retries a ifNum_retries>0: at PrintU'Crawl page error, after 10s continue to try .....' -Time.sleep (1) - returnSelf.get (url, timeout, False, num_retries-1) - Else:#The retry count is exhausted, using the proxy -Time.sleep (1) -IP =str (random.choice (self.iplist)) inProxy = {'http', IP} - returnSelf.get (url, timeout, true,5) to + Else:#using proxies - the Try: * Print 'Getting started with proxies' $IP =Random.choice (self.iplist)Panax NotoginsengProxy = {'http': IP} - Printproxy['http'] the returnRequests.get (url,headers=headers, timeout=timeout, proxies=Proxy) + except: a ifNum_retries>0: the PrintU'Agent Crawl page error, after 10s continue to try .....' +Time.sleep (1) - returnSelf.get (url, timeout,true, num_retries-1) $ Else:#not an agent either . $Time.sleep (30) - PrintU'Agent also useless ...' - returnNone the

HTTP request class based on Python third-party requests module

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.