Selenium(或 Chrome)检查 'Content-Type' 标头并选择要执行的操作。您也可以自己使用requests 来检查网址的'Content-Type',如下所示:
>>> r = requests.head('https://resus.org.au/?wpfb_dl=17')
>>> pprint.pprint(dict(r.headers))
{'Accept-Ranges': 'bytes',
'Age': '8518',
'Cache-Control': 'no-cache, must-revalidate, max-age=0',
'Connection': 'keep-alive',
'Content-Description': 'File Transfer',
'Content-Disposition': 'attachment; '
'filename="anzcor-guideline-6-compressions-apr-2021.pdf"',
'Content-Length': '535677',
'Content-Md5': '90AUQUZu0vFGJ7cBPvRxcg==',
'Content-Security-Policy': 'upgrade-insecure-requests',
'Content-Type': 'application/pdf',
'Date': 'Wed, 19 Jan 2022 11:20:06 GMT',
'Expires': 'Wed, 11 Jan 1984 05:00:00 GMT',
'Last-Modified': 'Wed, 19 Jan 2022 08:58:08 GMT',
'Pragma': 'no-cache',
'Server': 'openresty',
'Strict-Transport-Security': 'max-age=300, max-age=31536000; '
'includeSubDomains',
'Vary': 'User-Agent',
'X-Backend': 'local',
'X-Cache': 'cached',
'X-Cache-Hit': 'HIT',
'X-Cacheable': 'YES:Forced',
'X-Content-Type-Options': 'nosniff',
'X-Xss-Protection': '1; mode=block'}
如你所见,你的两个链接的'Content-Type'都是'application/pdf':
>>> r.headers['Content-Type']
'application/pdf'
所以你可以只检查requests.head(link).headers['Content-Type'] 的输出,然后做任何你需要的事情。
此时(2022 年 1 月 19 日),您问题中的第一个链接将我重定向到 404 页面。第二个仍然可以访问,但需要使用HTTPS协议,将链接的开始部分从http://更改为https://。
但无论如何,如果 URL 没有将您重定向到任何其他页面,则此答案不会过时。如果 URL 是,请通过检查 status_code 来请求最新的 URL,如果它是 301:
>>> r = requests.head('http://resus.org.au/?wpfb_dl=17')
>>> r.status_code
301
>>> r = requests.head('https://resus.org.au/?wpfb_dl=17')
>>> r.status_code
200
>>>