Python urllib/Requests下载文件失败,但浏览器下载失败

编程入门 行业动态 更新时间:2024-10-28 09:29:08
本文介绍了Python urllib/Requests下载文件失败,但浏览器下载失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试下载此zip file。 该压缩文件可通过Chrome正确下载,但使用请求或urllib失败,出现错误400 Bad Request。

>> import requests >> import urllib >> url = 'prd-enforce-xfr-02.dol.gov/../data_catalog/EBSA/ebsa_ocats_20150703.csv.zip' >> r = requests.get(url) >> r.ok False >> r.headers {'content-length': '254', 'content-encoding': 'gzip', 'vary': 'Accept-Encoding', 'server': 'Apache/2.2.14 (Ubuntu)', 'connection': 'close', 'date': 'Tue, 07 Jul 2015 20:39:55 GMT', 'content-type': 'text/html; charset=iso-8859-1'} >> r <Response [400]> >> r.text u'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>400 Bad Request</title> </head><body> <h1>Bad Request</h1> <p>Your browser sent a request that this server could not understand.<br /> </p> <hr> <address>Apache/2.2.14 (Ubuntu) Server at prd-enforce-xfr-02.dol.gov Port 80</address> </body></html> ' >> z = urllib.urlopen(url) >> z.read() '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>400 Bad Request</title> </head><body> <h1>Bad Request</h1> <p>Your browser sent a request that this server could not understand.<br /> </p> <hr> <address>Apache/2.2.14 (Ubuntu) Server at prd-enforce-xfr-02.dol.gov Port 80</address> </body></html> '

我尝试过的事情(失败了):

  • 欺骗用户代理标头。

  • 使用请求会话保存Cookie

推荐答案

删除无关的/..从URL。它可以在浏览器中工作,因为浏览器会为您标准化URL。如果没有/..这可以很好地处理urllib或请求。

更多推荐

Python urllib/Requests下载文件失败,但浏览器下载失败

本文发布于:2023-11-26 15:41:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浏览器   文件   Python   urllib   Requests

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!