[python3.4] urllib.request.urlretrieve(imgurl,'%s_%s.jpg'%(t,n)) 下载图片的时候报错:403 50
具体错误:Traceback(mostrecentcalllast):File"C:\Users\Haier\Desktop\pachong.py",line123,in...
具体错误:
Traceback (most recent call last):
File "C:\Users\Haier\Desktop\pachong.py", line 123, in <module>
urllib.request.urlretrieve(imgurl,'%s_%s.jpg'%(t,n))
File "E:\Python34\lib\urllib\request.py", line 178, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "E:\Python34\lib\urllib\request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "E:\Python34\lib\urllib\request.py", line 461, in open
response = meth(req, response)
File "E:\Python34\lib\urllib\request.py", line 571, in http_response
'http', request, response, code, msg, hdrs)
File "E:\Python34\lib\urllib\request.py", line 499, in error
return self._call_chain(*args)
File "E:\Python34\lib\urllib\request.py", line 433, in _call_chain
result = func(*args)
File "E:\Python34\lib\urllib\request.py", line 579, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
解决方法:
import requests
res = requests.get(imgurl)
with open(filename,'wb') as f:
f.write(res.content) 展开
Traceback (most recent call last):
File "C:\Users\Haier\Desktop\pachong.py", line 123, in <module>
urllib.request.urlretrieve(imgurl,'%s_%s.jpg'%(t,n))
File "E:\Python34\lib\urllib\request.py", line 178, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "E:\Python34\lib\urllib\request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "E:\Python34\lib\urllib\request.py", line 461, in open
response = meth(req, response)
File "E:\Python34\lib\urllib\request.py", line 571, in http_response
'http', request, response, code, msg, hdrs)
File "E:\Python34\lib\urllib\request.py", line 499, in error
return self._call_chain(*args)
File "E:\Python34\lib\urllib\request.py", line 433, in _call_chain
result = func(*args)
File "E:\Python34\lib\urllib\request.py", line 579, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
解决方法:
import requests
res = requests.get(imgurl)
with open(filename,'wb') as f:
f.write(res.content) 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏50(财富值+成长值)
1个回答
展开全部
试一下用浏览器直接访问图片网址,可以打开吗?
更多追问追答
追问
那肯定可以的呀
追答
在header里加一个user-agent试试。例如:
url = 'XXXX'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'}
r = requests.get(url, headers=headers)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询