|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 只为L红颜笑 于 2020-11-1 14:43 编辑
import urllib.request
opener = urllib.request.build_opener()
opener.addheader = {"User-Agent":' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36','Referer' : 'http://www.ysxs8.com'}
urllib.request.install_opener(opener)
url = "http://ysting.ysxs8.com:81/%E6%8E%A2%E9%99%A9%E7%9B%97%E5%A2%93/%E3%80%8A%E7%9B%97%E5%A2%93%E7%AC%94%E8%AE%B04%E8%9B%87%E6%B2%BC%E9%AC%BC%E5%9F%8E%E3%80%8B%E8%89%BE%E5%AE%9D%E8%89%AF/%E3%80%8A%E7%9B%97%E5%A2%93%E7%AC%94%E8%AE%B04%E8%9B%87%E6%B2%BC%E9%AC%BC%E5%9F%8E%E3%80%8B001--%E6%9C%89%E5%A3%B0%E5%B0%8F%E8%AF%B4%E5%90%A7%5Bwww.ysxs8.com%5D.mp3"
urllib.request.urlretrieve(url,"1.mp3" )
这里面的url在浏览器中可以打开 并且下载文件,但是在python的请求中会报错404 ,其他的网址可以直接用python访问,,,这个就不行 如果是我的headers没有正确 哪位大神可以帮我改一下吗 谢谢
报错代码如下:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\下载函数.py", line 13, in <module>
urllib.request.urlretrieve(url,"1.mp3" )
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 247, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 640, in http_response
response = self.parent.error(
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
>>>
|
|