urllib urllib2
Posted Howhy Blogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了urllib urllib2相关的知识,希望对你有一定的参考价值。
#-*-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()
以上是关于urllib urllib2的主要内容,如果未能解决你的问题,请参考以下文章
urllib2.urlopen() 与 urllib.urlopen() - urllib2 在 urllib 工作时抛出 404!为啥?
python中urllib, urllib2,urllib3, httplib,httplib2, request的区别