擅长:python、mysql、java
<p>它适用于stackoverflow,因为当您第一次访问端口80(http端口)上的stackoverflow.com时,stackoverflow的服务器会通知浏览器链接已永久移动到http s。</p>
<p>要在Python中检测到相同的内容,请使用<code>requests</code>库,如下所示:</p>
<pre><code>>>> import requests
>>> r = requests.get('http://stackoverflow.com') # first we try http
>>> r.url # check the actual URL for the site
'https://stackoverflow.com/'
</code></pre>
<p>要了解URL是如何更改的,请查看history对象,您将看到一个301响应,这意味着URI已经<a href="https://httpstatuses.com/301" rel="noreferrer">moved permanently</a>到了一个新地址。</p>
<pre><code>>>> r.history[0]
<Response [301]>
>>> r.history[0].url # this is the original URL we tried
'http://stackoverflow.com/'
</code></pre>