1
import requests
from bs4 import BeautifulSoup

url = "http://www.whatsmyip.org"

for x in range(0,5):
    response = requests.get(url).content

    soup = BeautifulSoup(response,'lxml')
    result = soup.findAll('h1')

    for each in result:
        print each.text
        break

Output:
Your IP Address is 19.12.86.57
Your IP Address is 151.138.87.69
Your IP Address is 108.206.165.11
Your IP Address is 148.84.71.226
Your IP Address is 50.201.205.131

当我运行这段代码时,我每次都会得到一个动态 IP,而不是我的公共 IP。谁能解释一下?

4

1 回答 1

1

我认为这不是关于 python,而是关于 whatsmyip.org ;) 他们可能检测并试图阻止脚本的某种方法。

尝试了其他一些网站,总是得到我的公共 IP。例子:

url = "https://www.iplocation.net"

for x in range(0,5):
   response = requests.get(url).content
   soup = BeautifulSoup(response, 'html.parser')
   result = soup.findAll('span')
   for each in result:
        try:
            if each.text[0] in '01234567890':
                print(each.text)
                break
        except:
            continue
于 2017-06-12T08:28:07.457 回答