#-*-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()