• Scrapy第十二篇:selenium4模拟器-本地代理


    1.正常使用selenium访问百度:调试模式会被浏览器检测到。

    1. import time
    2. from selenium import webdriver
    3. from selenium.webdriver.chrome.service import Service as ChromeService
    4. from webdriver_manager.chrome import ChromeDriverManager
    5. if __name__ == '__main__':
    6. # 初始化驱动
    7. service = ChromeService(executable_path=ChromeDriverManager().install())
    8. # 配置
    9. options = webdriver.ChromeOptions()
    10. # 获取浏览器实例
    11. driver = webdriver.Chrome(service=service, options=options)
    12. # 访问百度
    13. driver.get("https://www.baidu.com/")
    14. time.sleep(5)
    15. # 销毁实例
    16. driver.quit()

     

    2.使用代理浏览器selenium访问百度:真正的用户操作

    1. import os
    2. import time
    3. from selenium import webdriver
    4. from selenium.webdriver.chrome.service import Service as ChromeService
    5. from webdriver_manager.chrome import ChromeDriverManager
    6. if __name__ == '__main__':
    7. # 本地谷歌浏览器地址
    8. chrome_path = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    9. # 打开代理浏览器
    10. os.popen(rf'"{chrome_path}" --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"')
    11. # 初始化驱动
    12. service = ChromeService(executable_path=ChromeDriverManager().install())
    13. # 配置
    14. options = webdriver.ChromeOptions()
    15. # 配置代理参数
    16. options.add_experimental_option('debuggerAddress', 'localhost:9222')
    17. # 获取浏览器实例
    18. driver = webdriver.Chrome(service=service, options=options)
    19. # 访问百度
    20. driver.get("https://www.baidu.com/")
    21. time.sleep(5)
    22. # 销毁实例
    23. driver.quit()

    3.selenium本地代理中间件:

    1. class SeleniumMiddleware(object):
    2. def __init__(self):
    3. super().__init__()
    4. # 本地谷歌浏览器地址
    5. chrome_path = rf"C:\Users\Lenovo\AppData\Local\Google\Chrome\Application\chrome.exe"
    6. # 打开代理浏览器
    7. os.popen(rf'"{chrome_path}" --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"')
    8. def process_response(self, request, response, spider):
    9. # 初始化驱动
    10. service = ChromeService(executable_path=ChromeDriverManager().install())
    11. # 配置代理
    12. options = webdriver.ChromeOptions()
    13. options.add_experimental_option('debuggerAddress', '127.0.0.1:9222')
    14. # 获取浏览器实例
    15. driver = webdriver.Chrome(service=service, options=options)
    16. # 访问网页
    17. driver.get(request.url)
    18. # 最小化
    19. driver.minimize_window()
    20. # 全屏
    21. driver.maximize_window()
    22. # 指定大小
    23. driver.set_window_rect(0, 0, 1000, 500)
    24. # 获取关键字输入框
    25. input_element = WebDriverWait(driver, timeout=3).until(lambda d: d.find_element(By.ID, "kw"))
    26. # 输入搜索关键字
    27. input_element.send_keys("苍穹之跃" + str(random.randint(0, 9)))
    28. # 获取【百度一下】按钮
    29. search_button_element = WebDriverWait(driver, timeout=3).until(lambda d: d.find_element(By.ID, "su"))
    30. # 点击
    31. search_button_element.click()
    32. # 动态加载后的网页
    33. html = driver.page_source
    34. # 退出浏览器
    35. driver.quit()
    36. return scrapy.http.HtmlResponse(url=request.url, body=html.encode('utf-8'), encoding='utf-8', request=request)

    开启中间件:

    1. DOWNLOADER_MIDDLEWARES = {
    2. 'testproject.middlewares.SeleniumMiddleware': 543,
    3. }

    4.selenium本地代理中间件-多线程并发:

    未实现

  • 相关阅读:
    【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
    C++ 新特性 | C++ 11 | 移动语义与右值引用
    安装淘宝镜像cnpm报错
    低代码与数智制造:引领软件开发的革新之旅
    面试数据库篇(mysql)- 08事务
    AI生成PPT工具——Gamma,结合GPT生成不错的效果
    数据平台数据接入实践
    prometheus starting - 相识
    springboot整合pi支付开发
    java需要用到的工具类/方法(持续更新)
  • 原文地址:https://blog.csdn.net/wenxingchen/article/details/126371027