|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我仿照编了一个下载猫图片的程序,当输入 200*500 300*600之类的很快就写完了 但是 1000*1000j很慢 不知道什么原因 我之前用open写电脑内部复制视频都没事,这个才几百kB怎么这么慢,代码如下:
- import urllib.request
- url = 'http://placekitten.com/'
- #因为图片太多必须伪装报头
- while True:
- lenth = input('请输入长度:')
- width = input('请输入宽度:')
- if lenth == 'q' or width == 'q':
- break
- else:
- totalurl = url + lenth +'/'+ width
- req = urllib.request.Request(totalurl)
- req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')#伪装浏览器访问
- response = urllib.request.urlopen(req)
- print('报头:' + str(response.getheaders()))
- print('状态码:' + str(response.status))
- getfile = response.read()#只能读一次,再读就空了
- #写文件
- filename = '.\\pic\\'+ lenth + '_' + width + '.jpg'
- with open(filename,'wb')as f:
- f.write(getfile)
-
复制代码 |
|