爬虫进阶-反爬破解5(selenium的优势和点击操作+chrome的远程调试能力+通过Chrome隔离实现一台电脑登陆多个账号)

目录

一、selenium的优势和点击操作

二、chrome的远程调试能力

三、通过Chrome隔离实现一台电脑登陆多个账号


一、selenium的优势和点击操作

1.环境搭建

工具:Chrome浏览器+chromedriver+selenium

win用户:chromedriver.exe放在python.exe旁边

MacOS用户:驱动路径是/user/local/bin/chromedriver

Linux大佬自行安装

2.Selenium优势

Selenium直接操作浏览器,不需要分析请求和加密数据

程序可以读取网页源码,分析并提取内容

程序可以直接和网页元素进行交互,例如点击

python 复制代码
from selenium import webdriver
from time import sleep

url = 'http://shanzhi.spbeen.com/'
cb = webdriver.Chrome()
cb.get(url)
word_search_input = cb.find_element_by_xpath('.//input[@name="word"]')
word_search_input.send_keys("开发")
sleep(2)
search_button = cb.find_element_by_xpath('.//form[@action="/search/"]/button')
search_button.click() 
sleep(3)
num = 1
while num <= 5:
    next_element  = cb.find_element_by_xpath('.//div[@class="col-4"]/a[1]')
    next_element.click()
    sleep(3)
    num += 1
sleep(5)
cb.quit()

3.总结:

使用selenium,可以降低开发难度,提高开发效率

selenium可以直接操作页面元素,例如点击

selenium会降低程序运行速度,因为会主动加载更多的内容

二、chrome的远程调试能力

命令参数:--remote-debugging-port=9221

1.selenium端口调试的优势:

直接启动的浏览器,无selenium的特征,更安全

浏览器和selenium程序独立存在,不干扰

selenium依然可以控制chrome,程序上没有任何的修改

2.实践操作:selenium远程调试

Chrome开启远程调试端口

(1)windows用户

新建一个Chrome的快捷方式,然后鼠标右键,打开属性

(2)Mac (3)Linux

书写代码:

python 复制代码
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress","127.0.0.1:9221")
cb = webdriver.Chrome(options=options)

print(cb.title)

cb.get("https://www.zhihu.com")

print(cb.title)

cb.quit()

总结:

开启Chrome的远程调试端口,独立运行更自由

Selenium代码启动后,直接接管Chrome,操作没区别

注意Chrome网页环境参数问题,操作前先处理环境

三、通过Chrome隔离实现一台电脑登陆多个账号

1.启动参数介绍

--remote-debugging-port=9221

--user-data-dir=='C:/path_to/data_dir'

--headless

--window-size=1336,768

--disable-infobars

--incognito 无痕模式

2.正常模式和无痕模式

正常模式,数据正常保存并可以二次读取

无痕模式也会将数据存储在本地,不会二次加载

Selenium可以手动指定数据存储目录,用于多账号的数据存储

3.实践操作:Chrome数据存储隔离操作

python 复制代码
from selenium import webdriver
from time import sleep
import os

path = '/Users/buladou/chrome_temp_dir'

user = [
    ['demo123','demo123'],
    ['demo1234','demo1234'],
    ['test123','test123'],
    ['test1234','test1234']
]

for user in users:
    options = webdriver.ChromeOptions()
    user_path = os.path.join(path, user[0])
    if not os.path.exists(user_path):
        os.makedirs(user_path)
    if 'demo' in user[0]:
        options.add_argument("--user-data-dir={}".format(user_path))

    else:
        options.add_argument("--incognito")
    options.add_argument("--user-data-dir={}".format(user_path))
    cb = webdriver.Chrome(options=options)
    cb.get('http://shanzhi.spbeen.com/login/')
    username = cb.find_element_by_xpath('.//input[@name="username"]')
    username.send_keys(user[0])
    password =cb.find_element_by_xpath('.//input[@id="MemberPassword"]')
    password.send_keys(user[1])
sleep(60*60)

总结:

需要隐私操作,使用无痕模式启动浏览器,更保险

指定目录启动selenium,数据可以进行二次加载,读取记录

启动系统浏览器,有一个默认的存储地址,而且是固定的

相关推荐
傻啦嘿哟3 天前
高德地图POI爬虫:爬取商户与景点数据,做LBS应用
爬虫
weixin199701080163 天前
官方授权通道和灰色爬虫,做数据服务该选哪条路(附 python 源码)
c++·爬虫·python
正飞信息技术4 天前
llms.txt部署指南:给AI爬虫一张清晰的网站地图
人工智能·爬虫
NeoGressAI外贸数字化6 天前
外贸独立站robots.txt怎么写:6个AI爬虫放行规则
人工智能·爬虫
OnlineProxy6 天前
移动应用数据爬取:通过模拟器与流量嗅探对 API 进行逆向工程
爬虫·python·httpx
傻啦嘿哟6 天前
分布式爬虫架构:Redis+Scrapy打造企业级爬虫系统
分布式·爬虫·架构
梯度下降者6 天前
零基础入门Python 爬虫
爬虫·python·数据分析·入门指南·webcrawler
2601_967979127 天前
0 到 1 学爬虫:用 Python 爬取网页数据指南一(附实战案例)
爬虫·python·实战案例·html解析·网页数据
姚瑞南7 天前
【PRD】招投标商机雷达爬虫系统
人工智能·经验分享·笔记·爬虫·产品运营·产品经理
嫂子的姐夫8 天前
055-wasm:爬虫练习网spa15(token 纯算还原)
爬虫·wasm