本文介绍了Selenium Python改变IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium for Python编写一个Web scraper。刮刀每小时多次访问相同的站点,因此我希望每隔几次搜索就能找到一种方法来改变我的IP。什么是最好的策略(我使用的是Firefox)?是否有任何预先编写的代码/我可以切换的IP地址的csv?我完全不熟悉IP,代理等等。所以请放轻松我!

I am writing a web scraper using Selenium for Python. The scraper is visiting the same sites many times per hour, therefore I was hoping to find a way to alter my IP every few searches. What is the best strategy for this (I am using firefox)? Is there any prewritten code/a csv of IP addresses I can switch through? I am completely new to masking IP, proxies, etc. so please go easy on me!

推荐答案

尝试使用代理。
有免费选项(不太可靠)或付费服务。

Try using a proxy.There are free options (not so reliable) or payed services.

from selenium import webdriver

def change_proxy(proxy,port):
    profile = webdriver.FirefoxProfile();
    profile.set_preference("network.proxy.type", 1);
    profile.set_preference("network.proxy.http", proxy);
    profile.set_preference("network.proxy.http_port", port);
    profile.set_preference("network.proxy.ssl", proxy);
    profile.set_preference("network.proxy.ssl_port", port);
    driver = webdriver.Firefox(profile);
    return driver

这篇关于Selenium Python改变IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 11:10