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