简体   繁体   English

urllib.request.urlretrieve返回损坏的文件(如何处理这种网址?)

[英]urllib.request.urlretrieve returns corrupt file (How to handle this kind of url?)

I want to download about 1000 pdf files from a web page. 我想从网页下载大约1000个pdf文件。 Then I encountered this awkward pdf url format. 然后我遇到了这种尴尬的pdf url格式。 Both requests.get() and urllib.request.urlretrieve() don't work for me. urllib.request.urlretrieve() requests.get()urllib.request.urlretrieve()都不适用于我。

Usual pdf url looks like : 通常的pdf网址看起来像:

https://webpage.com/this_file.pdf

But this url is like : 但是这个网址就像:

https://gongu.copyright.or.kr/gongu/wrt/cmmn/wrtFileDownload.do?wrtSn=9000001&fileSn=1&wrtFileTy=01

So it doesn't have .pdf in url, and if you click on it, you can download it, But using python's urllib , you get corrupt file. 因此它在url中没有.pdf,如果你点击它,你可以下载它,但是使用python的urllib ,你会得到损坏的文件。

At first I thought it is redirected into some other url. 起初我以为它被重定向到其他一些网址。 So I used request.get(url, allow_retrieves=True) option, the result is the same url as before.. 所以我使用了request.get(url, allow_retrieves=True)选项,结果与之前的URL相同。

filename = './novel/pdf1.pdf'
url = 'https://gongu.copyright.or.kr/gongu/wrt/cmmn/wrtFileDownload.do?wrtSn=9031938&fileSn=1&wrtFileTy=01'

urllib.request.urlretrieve(url, filename)

this code downloads corrupt pdf file. 此代码下载损坏的pdf文件。

I solved it using content field in the retrieved object. 我使用检索到的对象中的内容字段解决了它。


filename = './novel1/pdf1.pdf'
url = . . .

object = requests.get(url)
with open('./novels/'+filename, 'wb') as f:
    f.write(t.content)

refered to this QnA ; 提到这个QnA; Download and save PDF file with Python requests module 使用Python请求模块下载并保存PDF文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python,基本问题:如何使用 urllib.request.urlretrieve 下载多个 url - Python, basic question: How do I download multiple url's with urllib.request.urlretrieve 如何在python 2.7中使用urllib.request.urlretrieve - How can I use urllib.request.urlretrieve with python 2.7 Python:urllib.request.urlretrieve保存一个空文件。 在其中写道“提供的id参数为空。” - Python: urllib.request.urlretrieve saves an empty file. Writes in it “Supplied id parameter is empty.” 如何在urllib.request.urlretrieve中添加标头以保留变量? - How can I add a header to urllib.request.urlretrieve keeping my variables? 我们如何从通过 urllib.request.urlretrieve 获取的 csv 中删除标头 - How can we remove header from csv being fetched via urllib.request.urlretrieve 使用urllib.request.urlretrieve下载需要固定的时间 - downloading with urllib.request.urlretrieve takes fixed time HTTP 错误 404:未找到 urllib.request.urlretrieve - HTTP Error 404: Not Found urllib.request.urlretrieve 使用什么命令代替 urllib.request.urlretrieve? - What command to use instead of urllib.request.urlretrieve? 无法在Python中使用“ urllib.request.urlretrieve”下载图像 - failing at downloading an image with “urllib.request.urlretrieve” in Python 单元测试模拟 urllib.request.urlretrieve() Python 3 和内部函数 - Unit test mock urllib.request.urlretrieve() Python 3 and internal function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM