【问题标题】:Why does python requests module give dynamic ip?为什么python请求模块给出动态ip?
【发布时间】:2016-08-24 08:27:42
【问题描述】:
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。谁能解释一下?

【问题讨论】:

  • 你使用类似tor的东西吗?
  • 不,我不是。只是对这个响应感到困惑(输出)
  • 我也一样,你找到原因了吗?

标签: python-requests dynamic-ip


【解决方案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

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 2017-11-15
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    相关资源
    最近更新 更多