Set timeout exception processing and proxy IP's

import requests
import re

# To obtain this IP

url='http://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=ip'

res=requests.get(url)
res.encoding='utf-8'
pat='<span class="c-gap-right">本机IP:&nbsp;(.*?)</span>'
ip=re.findall(pat,res.text)
print(ip)

 

#https: //www.xicidaili.com/ West thorns proxy IP
ip = '60 .191.57.82: 53832 '# may need to replace the unsuccessful
res1 = requests.get (url, proxies = {' http ':' http: // '} + IP)
res1.encoding =' UTF-. 8 '
PAT1 =' <span class = "C-GAP-right"> native the IP:.? & nbsp; (*) </ span> '
IP = the re.findall (pat1, res1.text)

print(ip)

 

# Timeout exception processing setting timeout
URL = 'HTTP: //www.baidu.com'

for i in range(10):
try:
res=requests.get(url,timeout=0.025)
print(res)
except Exception as e:
print(e)

 

Guess you like

Origin www.cnblogs.com/tiankong-blue/p/11601308.html