Python header 请求内容错误 type/Error 404
Python header request wrong content type/Error 404
我想通过检查 header 请求的内容类型来找出文件格式。
我下载了一个 csv 文件 link。如果我检查内容类型,它显示 text/html,如果我尝试 运行 status_code,我会收到错误 404.
如果我在终端中使用 curl -v 运行 它,我会得到正确的内容类型。
有人知道如何在 python 中解决这个问题吗?
提前致谢!
这是我目前所做的:
import requests
url = "https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GDP.../OECD?contentType=csv&detail=code&separator=comma&csv-lang=en"
r = requests.head(url)
headerDict=r.headers
print(headerDict)
我回来了:
{'Cache-Control': 'private', 'Content-Length': '4941', 'Content-Type': 'text/html; charset=utf-8', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Allow-Methods': 'POST,GET,OPTIONS', 'Date': 'Fri, 22 Oct 2021 08:54:31 GMT', 'Connection': 'close'}
您可以使用它来获取内容类型
import requests
import mimetypes
response = requests.get('https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GDP.../OECD?contentType=csv&detail=code&separator=comma&csv-lang=en')
content_type = response.headers['content-type']
extension = mimetypes.guess_extension(content_type)
print(content_type)
print(extension)
输出
text/csv
.csv
我想通过检查 header 请求的内容类型来找出文件格式。 我下载了一个 csv 文件 link。如果我检查内容类型,它显示 text/html,如果我尝试 运行 status_code,我会收到错误 404.
如果我在终端中使用 curl -v 运行 它,我会得到正确的内容类型。 有人知道如何在 python 中解决这个问题吗? 提前致谢!
这是我目前所做的:
import requests
url = "https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GDP.../OECD?contentType=csv&detail=code&separator=comma&csv-lang=en"
r = requests.head(url)
headerDict=r.headers
print(headerDict)
我回来了:
{'Cache-Control': 'private', 'Content-Length': '4941', 'Content-Type': 'text/html; charset=utf-8', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Allow-Methods': 'POST,GET,OPTIONS', 'Date': 'Fri, 22 Oct 2021 08:54:31 GMT', 'Connection': 'close'}
您可以使用它来获取内容类型
import requests
import mimetypes
response = requests.get('https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GDP.../OECD?contentType=csv&detail=code&separator=comma&csv-lang=en')
content_type = response.headers['content-type']
extension = mimetypes.guess_extension(content_type)
print(content_type)
print(extension)
输出
text/csv
.csv