[英]Can't get redicect URL with python requests
import requests
print(requests.get("http://zfxxgk.nea.gov.cn/2022-01/17/c_1310427545.htm").url)
#returns 'http://zfxxgk.nea.gov.cn/2022-01/17/c_1310427545.htm'
上面的代碼返回與我傳遞的相同的 url,但 url 實際上重定向到另一個 url。如何獲得重定向后的 url? 謝謝
卷曲電話
curl http://zfxxgk.nea.gov.cn/2022-01/17/c_1310427545.htm
你得到的源女巫包含 furas 提到的 javascript-redirect:
<script language="javascript" type="text/javascript">window.location.href="http://zfxxgk.nea.gov.cn/2021-12/31/c_1310427545.htm";</script>
如果您獲得“真實”重定向,您將在 header 中找到新的“位置”:
curl -I http://something.somewhere
HTTP/1.1 301 Moved Permanently
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://inder.net
如果您需要使用 python 進行自動化,您應該查看 request.history,如對類似問題的此答案中所述,以查找您的呼叫觸發的所有重定向: “Python Requests library redirect new url”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.