如何在HTMLURL生成下载时学习PDF的内容类型
How to learn content-type of PDF when HTML URL generates download
我正在尝试使用 requests
content-type
确定 URL 的 MIME 类型是否为 PDF 文件。
如果查询 URL 实际上指向托管的 PDF 文件,则以下脚本可以正常工作。但是,对于下面的当前 URL,content-type
被检测为 text/html; charset=utf-8
,即使它导致强制从网络下载 PDF 文件(文件本身在 [=27 中不可读) =] 直接来自 URL).
import requests
url = 'https://derinet.vontobel.ch/api/kid?isin=DE000VS0URS6&language=en'
def getContentType(url):
r = requests.Session().head(url, allow_redirects=True)
contentType = r.headers['content-type']
print 'Content type: ' + contentType
if (contentType == 'application/pdf') | (contentType == 'application/x-pdf'):
print "content-type is PDF"
else:
print "content-type is not PDF!"
getContentType(url)
有没有办法检查实际生成的 PDF 下载的 MIME 类型,而不是生成它的 html 页面(blob??)?
我设置了allow_redirects=True
,不过这里好像没关系
如果您没有发出 HEAD
请求,而是发出 GET
请求,您将得到您正在寻找的 header:
$ python
Python 3.7.2 (default, Feb 12 2019, 08:15:36)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> url = 'https://derinet.vontobel.ch/api/kid?isin=DE000VS0URS6&language=en'
>>> r = requests.get(url)
>>> r.headers
{
'Date': 'Wed, 29 May 2019 14:26:56 GMT',
'Server': 'Apache',
'Set-Cookie': 'AL_LB=$xc/70TgziJYWEd9zZwhLxXFDJHocFA0gS9gdbEQS0N0LhFme3uS; Path=/; HTTPOnly; Domain=.vontobel.ch',
'Content-Length': '51764',
'Cache-Control': 'private',
'Content-Disposition': 'attachment; filename=KID_DE000VS0URS6_en.pdf',
'X-Frame-Options': 'SAMEORIGIN',
'Pragma': 'blah',
'Vary': 'User-Agent',
'Keep-Alive': 'timeout=2, max=500',
'Connection': 'Keep-Alive',
'Content-Type': 'application/pdf',
}
header 的区别是由服务器决定的,所以我认为没有 GET
请求就无法获得正确的 header。
我正在尝试使用 requests
content-type
确定 URL 的 MIME 类型是否为 PDF 文件。
如果查询 URL 实际上指向托管的 PDF 文件,则以下脚本可以正常工作。但是,对于下面的当前 URL,content-type
被检测为 text/html; charset=utf-8
,即使它导致强制从网络下载 PDF 文件(文件本身在 [=27 中不可读) =] 直接来自 URL).
import requests
url = 'https://derinet.vontobel.ch/api/kid?isin=DE000VS0URS6&language=en'
def getContentType(url):
r = requests.Session().head(url, allow_redirects=True)
contentType = r.headers['content-type']
print 'Content type: ' + contentType
if (contentType == 'application/pdf') | (contentType == 'application/x-pdf'):
print "content-type is PDF"
else:
print "content-type is not PDF!"
getContentType(url)
有没有办法检查实际生成的 PDF 下载的 MIME 类型,而不是生成它的 html 页面(blob??)?
我设置了allow_redirects=True
,不过这里好像没关系
如果您没有发出 HEAD
请求,而是发出 GET
请求,您将得到您正在寻找的 header:
$ python
Python 3.7.2 (default, Feb 12 2019, 08:15:36)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> url = 'https://derinet.vontobel.ch/api/kid?isin=DE000VS0URS6&language=en'
>>> r = requests.get(url)
>>> r.headers
{
'Date': 'Wed, 29 May 2019 14:26:56 GMT',
'Server': 'Apache',
'Set-Cookie': 'AL_LB=$xc/70TgziJYWEd9zZwhLxXFDJHocFA0gS9gdbEQS0N0LhFme3uS; Path=/; HTTPOnly; Domain=.vontobel.ch',
'Content-Length': '51764',
'Cache-Control': 'private',
'Content-Disposition': 'attachment; filename=KID_DE000VS0URS6_en.pdf',
'X-Frame-Options': 'SAMEORIGIN',
'Pragma': 'blah',
'Vary': 'User-Agent',
'Keep-Alive': 'timeout=2, max=500',
'Connection': 'Keep-Alive',
'Content-Type': 'application/pdf',
}
header 的区别是由服务器决定的,所以我认为没有 GET
请求就无法获得正确的 header。