python 3中urlretrieve方法直接将远程数据下载到本地.为什么不行?
importreimporturllib.requestdefgetHtml(url):page=urllib.request.urlopen(url)html=page...
import re
import urllib.request
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
def getImg(html):
html = html.decode('GBK')
reg = r'src="(.*?\.jpg)" width'
imgre = re.compile(reg)
imglist = imgre.findall(html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpg' % x)#是不是Python3.X中把这个也改变了?
x += 1
html = getHtml('http://tieba.baidu.com/p/741081023')
print(getImg(html)) 展开
import urllib.request
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
def getImg(html):
html = html.decode('GBK')
reg = r'src="(.*?\.jpg)" width'
imgre = re.compile(reg)
imglist = imgre.findall(html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpg' % x)#是不是Python3.X中把这个也改变了?
x += 1
html = getHtml('http://tieba.baidu.com/p/741081023')
print(getImg(html)) 展开
2018-02-02
展开全部
你的路径可能不对,按照我的代码,你需要在d盘根目录下新建一个test文件夹,才能正常运行
我把代码附上:
import re
import urllib.request
def Schedule(a,b,c):
per = 100.0 * a * b / c
if per>100:
per = 100
print('完成!')
print('%.2f%%' % per)
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
def getImg(html):
html = html.decode('utf-8')
reg = r'src="(.*?\.jpg)" width'
imgre = re.compile(reg)
imglist = imgre.findall(html)
x = 0
for imgurl in imglist:
# 写好你的路径
urllib.request.urlretrieve(imgurl,'D:\\test\\%s.jpg' % x,Schedule)
x += 1
html = getHtml('http://tieba.baidu.com/p/741081023')
print(getImg(html))
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询