爬虫自动化之drissionpage实现随时切换代理ip
https://blog.csdn.net/qq_32334103/article/details/126133862
下载 SwitchyOmega地址: https://github.com/FelisCatus/SwitchyOmega/releases
也可以直接用压缩文件打开,然后解压到当前文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import platform
from DrissionPage import ChromiumPage, ChromiumOptions
from loguru import logger
def get_free_ip():
url = "https://www.zdaye.com/free/?ip=&adr=&checktime=&sleep=&cunhuo=&dengji=1&nadr=&https=&yys=&post=&px="
browser.get(url, retry = 3 , interval = 1 , timeout = 15 )
ip_ports = []
for tr in browser.eles( 'x://table[@id="ipc"]//tr' )[ 1 :]:
tds = [td.text for td in tr.eles( "x://td" )]
ip_ports.append((f "{tds[0]}:{tds[1]}" , tds[ 3 ]))
print ( len (ip_ports), ip_ports)
return ip_ports
def switch_ip(ip_port = None ):
global set_proxy
if ip_port:
# 设置proxy
ip, port = ip_port.split( ":" )
tab = browser.new_tab()
tab.get( "chrome-extension://padekgcemlokbadohgkifijomclgjgif/options.html#!/profile/proxy" )
tab.ele( 'x://input[@ng-model="proxyEditors[scheme].host"]' ). input (ip, clear = True )
tab.ele( 'x://input[@ng-model="proxyEditors[scheme].port"]' ). input (port, clear = True )
tab.ele( 'x://a[@ng-click="applyOptions()"]' ).click()
tab.wait( 1 )
# 提示框
txt = tab.handle_alert()
print ( "提示框" , txt)
tab.handle_alert(accept = False )
if not omega_proxy:
# 切换proxy
tab.get( "chrome-extension://padekgcemlokbadohgkifijomclgjgif/popup/index.html#" )
tab.wait( 1 )
tab.ele( 'x://span[text()="proxy"]' ).click()
set_proxy = True
else :
tab = browser.new_tab()
tab.get( "chrome-extension://padekgcemlokbadohgkifijomclgjgif/popup/index.html#" )
tab.ele( 'x://span[text()="[直接连接]"]' ).click()
if len (browser.tab_ids) > 1 :
print ( "当前tab个数" , len (browser.tab_ids))
tab.close()
if platform.system().lower() = = 'windows' :
co = ChromiumOptions() # .set_paths(browser_path=r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
else :
co = ChromiumOptions().set_paths(browser_path = r "/opt/google/chrome/google-chrome" )
co.headless( True ) # 设置无头加载 无头模式是一种在浏览器没有界面的情况下运行的模式,它可以提高浏览器的性能和加载速
# co.incognito(True) # 无痕隐身模式打开的话,不会记住你的网站账号密码的
co.set_argument( '--no-sandbox' ) # 禁用沙箱 禁用沙箱可以避免浏览器在加载页面时进行安全检查,从而提高加载速度 默认情况下,所有Chrome 用户都启用了隐私沙盒选项 https://zhuanlan.zhihu.com/p/475639754
co.set_argument( "--disable-gpu" ) # 禁用GPU加速可以避免浏览器在加载页面时使用过多的计算资源,从而提高加载速度
co.set_user_agent(user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36' ) # 设置ua
co.set_timeouts( 6 , 6 , 6 )
co.set_local_port( 9211 )
# 1、设置switchyOmega插件
co.add_extension(r 'C:\Users\Ad\Desktop\proxy_switchyomega-2.5.20' )
browser = ChromiumPage(co)
# 2、重置switchyOmega插件
omega_proxy = False
switch_ip()
browser.get( "https://www.ip138.com/" , retry = 0 )
html_text = browser.get_frame( 'x://div[@class="hd"]//iframe' ).ele( 'text:您的iP地址是' ).text
logger.success(f ">>>>当前的ip {html_text}" )
# 3、随机切换代理ip
# ip_all = get_free_ip()
ip_all = [{ "ip" : "10.1.3.56" , "port" : 7890 , "expire_time" : "2024-04-27 22:24:00" }]
for ips in ip_all:
logger.info(f "~~~切换ip,now {ips['ip']}" )
# 重置switchyOmega插件
switch_ip(f "{ips['ip']}:{ips['port']}" )
browser.wait( 1 )
try :
browser.get( "https://www.baidu.com/" , retry = 0 )
browser.get( "https://www.ip138.com/" , retry = 0 )
browser.get( "https://www.google.com/" , retry = 0 )
html_text = browser.get_frame( 'x://div[@class="hd"]//iframe' ).ele( 'text:您的iP地址是' ).text
logger.success(f ">>>>>>>>切换代理成功 {html_text}" )
except Exception as err:
logger.error(f "----------切换代理失败 dp {err}" )
browser.wait( 10 )
browser.quit()
|