requests庫優於urllib,其基於urllib。cookie
urllib我的基本掌握。ui
1 #urllib.request 2 r=urllib.request.urlopen('http://www.cnblogs.com/realmonkeykingsun/') 3 print(r.read().decode('utf-8)) 4 5 #urllib.parser 6 data=(urllib.parser.urlencode({'world':'hello'}),encoding='utf-8) 7 r=urllib.request.urlopen('http://www.cnblogs.com/realmonkeykingsun/','data=data) 8 9 #urllib.error 10 try: 11 r=urllib.request.urlopen('http://www.cnblogs.com/realmonkeykingsun/',timeout=0.1) 12 except urllib.error.URLErrur as e: 13 print(e)
Requesturl
import urllib.request request=urllib.request.Request(url=url,data=data,header=header) r=urlllib.request.urlopen(request)
Handlerspa
#代理 import urllib.request proxy_handler=urllib.request.ProxyHandler({'http':'http://127.0.1:9743'} opener=urllib.request.build_opener(proxy_handler) r=opener.open('http://www.baidu.com') #Cookie import http.cookiejar,urllib.request cookie=http:cookiejar.CookieJar() handler=urllib.request.HTTPCookieProcessor(cookie) opener=urllib.request.build_opener(handler) r=opener.open("http") for item in cookie: print(item.name+"="item.value)
URL解析代理
#urlparser urlunparse result=urlparse("http") data=['http','user','a-6'] urlunparse(data) #urljon urljoin(h1,h2) #urlencode params={'name':'germey','age':'23'} base_url='http1‘ url=base_url+urlencode(params)