urllib urllib2
阿新 • • 發佈:2017-06-16
all ret chrome char import web http url res
#-*-coding:utf-8-*-
import urllib
import urllib2
import cookielib
##urllib
url="http://www.qq.com"
header={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"}
def urllib_study(url): urldata=urllib.urlopen(url) print(urldata.read().decode(‘gbk‘))##網頁內容 print(urldata.getcode())##http狀態碼 print(urldata.geturl())## request url print(urldata.info()) ##header info urldata.info().getparam(‘charset‘)獲取header 編碼 urllib.urlretrieve(url,‘D:\\pic\\‘)##下載文件 def urlretrieve_study(url,savepath): def callback(a,b,c): down_progess=100.0*a*b/c if down_progess>100: down_progess=100 print ‘%.2f%%‘%down_progess urllib.urlretrieve(url,savepath,callback)
#urlretrieve_study(url,‘D:\\pics\\qq.html‘) ##urllib2 def urllib2_study(url): urldata=urllib2.urlopen(url) print(dir(urldata))##urldata.read.decode(‘gbk‘) urldata.getcode() urldata.info() urldata.geturl() cookie=cookielib.CookieJar() opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) data=opener.open(url)##urllib2.urlopen其實也是調用opener.open print(data.read().decode(‘gbk‘)) def urllib2_post(url): values={‘name‘:‘howhy‘,‘age‘:32} data=urllib.urlencode(values) req=urllib2.Request(url,data,header) response=urllib2.urlopen(req) the_page=response.read()
def urllib_study(url): urldata=urllib.urlopen(url) print(urldata.read().decode(‘gbk‘))##網頁內容 print(urldata.getcode())##http狀態碼 print(urldata.geturl())## request url print(urldata.info()) ##header info urldata.info().getparam(‘charset‘)獲取header 編碼 urllib.urlretrieve(url,‘D:\\pic\\‘)##下載文件 def urlretrieve_study(url,savepath): def callback(a,b,c): down_progess=100.0*a*b/c if down_progess>100: down_progess=100 print ‘%.2f%%‘%down_progess urllib.urlretrieve(url,savepath,callback)
#urlretrieve_study(url,‘D:\\pics\\qq.html‘) ##urllib2 def urllib2_study(url): urldata=urllib2.urlopen(url) print(dir(urldata))##urldata.read.decode(‘gbk‘) urldata.getcode() urldata.info() urldata.geturl() cookie=cookielib.CookieJar() opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) data=opener.open(url)##urllib2.urlopen其實也是調用opener.open print(data.read().decode(‘gbk‘)) def urllib2_post(url): values={‘name‘:‘howhy‘,‘age‘:32} data=urllib.urlencode(values) req=urllib2.Request(url,data,header) response=urllib2.urlopen(req) the_page=response.read()
urllib urllib2