Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在HTML URL生成下载时了解PDF的内容类型_Python_Python 2.7_Python Requests_Mime Types_Content Type - Fatal编程技术网

Python 如何在HTML URL生成下载时了解PDF的内容类型

Python 如何在HTML URL生成下载时了解PDF的内容类型,python,python-2.7,python-requests,mime-types,content-type,Python,Python 2.7,Python Requests,Mime Types,Content Type,我正在尝试使用请求内容类型来确定URL的MIME类型是否为PDF文件 如果查询的URL实际指向托管的PDF文件,则下面的脚本可以正常工作。但是,在下面的当前URL中,内容类型被检测为文本/html;charset=utf-8即使它会导致强制从web下载PDF文件(文件本身在Chrome中无法直接从URL读取) 有没有办法检查实际生成的PDF下载的MIME类型,而不是生成它的html页面(blob 我已经设置了allow\u redirects=True,但这似乎并不重要。如果您没有发出HEAD请

我正在尝试使用
请求
内容类型
来确定URL的MIME类型是否为PDF文件

如果查询的URL实际指向托管的PDF文件,则下面的脚本可以正常工作。但是,在下面的当前URL中,
内容类型
被检测为
文本/html;charset=utf-8
即使它会导致强制从web下载PDF文件(文件本身在Chrome中无法直接从URL读取)

有没有办法检查实际生成的PDF下载的MIME类型,而不是生成它的html页面(blob


我已经设置了
allow\u 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',
}

头的差异是由服务器决定的,因此我认为没有
get
请求就无法获得正确的头。

Pearhaps您可以使用r.history来查找您要查找的内容。
$ 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',
}