请求具有中文ch的url

2024-04-25 14:39:47 发布

您现在位置:Python中文网/ 问答频道 /正文

正在尝试获取以下url上的内容,但不使用请求模块。尽管链接在浏览器上打开。如何使用请求库获取链接

In [2]: requests.get('http://www.dwconstir.com/inc/download.asp?FileName=3Q17%20%uC2E4%uC801PT_ENG.pdf')
Out[2]: <Response [400]>

Tags: 模块incomhttpurl内容get链接
1条回答
网友
1楼 · 发布于 2024-04-25 14:39:47

您正在尝试下载PDF。您可以使用urllib2

样本

import urllib2

src_url = "http://www.dwconstir.com/inc/download.asp?FileName=3Q17%20%uC2E4%uC801PT_ENG.pdf"
path = "DEST_PATH"  #Folder location where you want to download the file.
response = urllib2.urlopen(src_url)
file = open(path + "document.pdf", 'wb')
file.write(response.read())
file.close()

使用请求

import requests
url = 'http://www.dwconstir.com/inc/download.asp?FileName=3Q17%20%uC2E4%uC801PT_ENG.pdf'
path = "DEST_PATH"  #Folder location where you want to download the file.
r = requests.get(url, stream=True)

with open(path + "document.pdf", 'wb') as f:
    f.write(r.content)

相关问题 更多 >