【问题标题】:How to verify if the IP is replaced with proxy server(selenium, python)如何验证IP是否替换为代理服务器(selenium,python)
【发布时间】:2018-01-30 11:10:55
【问题描述】:

如何验证实际 IP 地址是否被代理服务器替换。我附上了代码。我用 selenium 和 python 制作了这个程序。

# selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random

PROXY ="88.157.149.250:8080";


chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
# //a[starts-with(@href, 'https://www.amazon.com/')]/@href
LINKS_XPATH = '//*[contains(@id,"result")]/div/div[3]/div[1]/a'
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get(
    'https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011')
links = browser.find_elements_by_xpath(LINKS_XPATH)
for link in links:
    href = link.get_attribute('href')
    print(href)

【问题讨论】:

    标签: python selenium proxy


    【解决方案1】:

    获取类似https://api.ipify.org?format=json的网页

    您也可以构建自己的服务,只打印请求的源 IP...

    from flask import request
    from flask import jsonify
    
    app = Flask(__name__)
    
    @app.route("/get_my_ip", methods=["GET"])
    def get_my_ip():
        return jsonify({'ip': request.remote_addr})
    
    if __name__ == "__main__":
        app.run(debug = False, host = '0.0.0.0', port = 80, threaded = True)
    

    【讨论】:

    • 干净有效 (+1)
    • 要不要加这行代码:app = Flask(name)?
    • 哦,是的,当然,我已经用完全运行的烧瓶服务器编辑了我的答案,但没有测试,让我知道这是否适合你:)
    • 我添加了 Flask(到要导入的第一行)并返回:Running on 0.0.0.0:80(按 CTRL+C 退出)
    • 是的,服务器正在运行,现在使用您的其他脚本,您可以使用 selenium 通过代理查询您的外部 IP 地址,并查看您获得的 IP。
    【解决方案2】:

    只需访问一个显示它的页面,就像 Loïc 显示的那样。然后,您继续访问亚马逊或任何您想要的网站,知道您使用的是正确的 IP 地址。

    from selenium.webdriver.common.by import By
    
    driver.get('https://api.ipify.org/')
    ip_address = navigator.driver.find_element(By.TAG_NAME, "body").text
    print(ip_address)
    

    【讨论】:

      猜你喜欢
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      相关资源
      最近更新 更多