码迷,mamicode.com
首页 > Web开发 > 详细

urllib urllib2

时间:2017-06-16 16:46:59      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:lda   proc   header   x64   use   htm   chrome   inf   urlencode   

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

标签:lda   proc   header   x64   use   htm   chrome   inf   urlencode   

原文地址:http://www.cnblogs.com/howhy/p/7027681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!