为什么python请求模块提供动态ip?

时间:2016-04-29 08:56:28

标签: python-requests dynamic-ip

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。任何人都可以解释一下吗?

1 个答案:

答案 0 :(得分: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