生成有效 IP 地址列表并在 Python Selenium 循环中随机使用
Generate List of Valid IP Addresses & Randomly Use in Python Selenium Loop
免责声明:这是我第一次涉足网络抓取
我有一个包含约 400 个搜索结果 URL 的列表,我正尝试使用 Selenium 循环这些 URL 来收集信息。在某个时候,我被重定向并显示以下文本:
"Your access to VINELink.com has been declined due to higher than normal utilization levels... You are attempting to access this website from the following ip address. Please make sure your firewall settings are not restricting access. [MY IP ADDRESS]"
有没有办法生成一个有效的随机 IP 地址列表,select 在一个循环中随机生成一个并将其提供给 Selenium WebDriver 以避免被阻止?
我明白这个问题有道德方面的考虑(实际上,我已经联系该网站解释我的良性用例并询问他们是否可以解封我的真实 IP 地址);我主要只是想知道这是不是有人可以做的事情。
URL 的缩写列表:
['http://www.vinelink.com/vinelink/servlet/SubjectSearch?siteID=34003&agency=33&offenderID=2662',
'http://www.vinelink.com/vinelink/servlet/SubjectSearch?siteID=34003&agency=33&offenderID=A21069',
'http://www.vinelink.com/vinelink/servlet/SubjectSearch?siteID=34003&agency=33&offenderID=B59293',
...]
循环的缩写代码(缺少有效 IP 地址的实际列表):
info = {}
for url in detail_urls:
proxy = ### SELECT RANDOM IP ADDRESS FROM A LIST OF VALID IP ADDRESSES ###
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server='+str(proxy))
driver = webdriver.Chrome(executable_path='/PATH/chromedriver', options=chrome_options)
driver.get(url)
driver.implicitly_wait(3)
if drive.find_element_by_xpath('//*[@id="ngVewDiv"]/div/div/div/div[3]/div[3]/div[2]/div/search-result/div/div[4]/div[1]/more-info/div[1]/button'):
button = driver.find_element_by_xpath('//*[@id="ngVewDiv"]/div/div/div/div[3]/div[3]/div[2]/div/search-result/div/div[4]/div[1]/more-info/div[1]/button').click()
name = driver.find_element_by_xpath('//*[@id="ngVewDiv"]/div/div/div/div[3]/div[3]/div[2]/div/search-result/div/div[1]/div/div[1]/span[1]/span[1]/div/div/div[2]/span')
name = name.text
offenderid = driver.find_element_by_xpath('//*[@id="ngVewDiv"]/div/div/div/div[3]/div[3]/div[2]/div/search-result/div/div[4]/div[1]/more-info/div[2]/div/div/div[2]/div[1]/div/div[2]/span')
offenderid = offenderid.text
info[name] = [offenderid]
driver.close()
else:
driver.close()
Is there a way to generate a list of valid random IP addresses,
select one randomly within a loop and feed it to the Selenium
WebDriver to avoid being blocked?
要从序列中获取随机项目,请使用 random
模块中的 random.choice(seq)
。
参见:https://docs.python.org/3/library/random.html#random.choice
示例:
import random
proxies = ['10.0.1.1', '10.0.1.2', '10.0.1.3']
proxy = random.choice(proxies)
注意:
您的问题有点没有意义,因为您声明要生成 valid IP 地址列表。您不能只生成随机 IP 并期望它们能够工作……您必须实际向您的脚本提供有效的 IP。您将需要提供此服务的服务器基础架构(即绑定到列表中每个地址的工作代理服务器池),因为请求将通过这些服务器进行路由。如果您只是想欺骗您的 IP 并且没有要代理的服务器池,答案是 "No, that won't work."
免责声明:这是我第一次涉足网络抓取
我有一个包含约 400 个搜索结果 URL 的列表,我正尝试使用 Selenium 循环这些 URL 来收集信息。在某个时候,我被重定向并显示以下文本:
"Your access to VINELink.com has been declined due to higher than normal utilization levels... You are attempting to access this website from the following ip address. Please make sure your firewall settings are not restricting access. [MY IP ADDRESS]"
有没有办法生成一个有效的随机 IP 地址列表,select 在一个循环中随机生成一个并将其提供给 Selenium WebDriver 以避免被阻止?
我明白这个问题有道德方面的考虑(实际上,我已经联系该网站解释我的良性用例并询问他们是否可以解封我的真实 IP 地址);我主要只是想知道这是不是有人可以做的事情。
URL 的缩写列表:
['http://www.vinelink.com/vinelink/servlet/SubjectSearch?siteID=34003&agency=33&offenderID=2662',
'http://www.vinelink.com/vinelink/servlet/SubjectSearch?siteID=34003&agency=33&offenderID=A21069',
'http://www.vinelink.com/vinelink/servlet/SubjectSearch?siteID=34003&agency=33&offenderID=B59293',
...]
循环的缩写代码(缺少有效 IP 地址的实际列表):
info = {}
for url in detail_urls:
proxy = ### SELECT RANDOM IP ADDRESS FROM A LIST OF VALID IP ADDRESSES ###
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server='+str(proxy))
driver = webdriver.Chrome(executable_path='/PATH/chromedriver', options=chrome_options)
driver.get(url)
driver.implicitly_wait(3)
if drive.find_element_by_xpath('//*[@id="ngVewDiv"]/div/div/div/div[3]/div[3]/div[2]/div/search-result/div/div[4]/div[1]/more-info/div[1]/button'):
button = driver.find_element_by_xpath('//*[@id="ngVewDiv"]/div/div/div/div[3]/div[3]/div[2]/div/search-result/div/div[4]/div[1]/more-info/div[1]/button').click()
name = driver.find_element_by_xpath('//*[@id="ngVewDiv"]/div/div/div/div[3]/div[3]/div[2]/div/search-result/div/div[1]/div/div[1]/span[1]/span[1]/div/div/div[2]/span')
name = name.text
offenderid = driver.find_element_by_xpath('//*[@id="ngVewDiv"]/div/div/div/div[3]/div[3]/div[2]/div/search-result/div/div[4]/div[1]/more-info/div[2]/div/div/div[2]/div[1]/div/div[2]/span')
offenderid = offenderid.text
info[name] = [offenderid]
driver.close()
else:
driver.close()
Is there a way to generate a list of valid random IP addresses, select one randomly within a loop and feed it to the Selenium WebDriver to avoid being blocked?
要从序列中获取随机项目,请使用 random
模块中的 random.choice(seq)
。
参见:https://docs.python.org/3/library/random.html#random.choice
示例:
import random
proxies = ['10.0.1.1', '10.0.1.2', '10.0.1.3']
proxy = random.choice(proxies)
注意: 您的问题有点没有意义,因为您声明要生成 valid IP 地址列表。您不能只生成随机 IP 并期望它们能够工作……您必须实际向您的脚本提供有效的 IP。您将需要提供此服务的服务器基础架构(即绑定到列表中每个地址的工作代理服务器池),因为请求将通过这些服务器进行路由。如果您只是想欺骗您的 IP 并且没有要代理的服务器池,答案是 "No, that won't work."