当前位置: 首页 > article >正文

[手机Linux PostmarketOS]七, Linux使用selenium爬虫

一,selenium安装

# 用pip 安装 selenium
pip3 install selenium --break-system-packages

二,安装浏览器Chrome

    Alpine Linux 环境中没有google Chrome, 使用 Chromium 浏览器作为 Chrome 的替代品,Chromium 是 Chrome 的开源版本,可以在 Alpine Linux 上安装。

sudo apk add chromium

安装完后:
chromium --version   #查看版本

chromium --help

三,安装Chromedriver

    Alpine Linux 使用的是 musl libc 和 busybox,而不是 glibc,因此它与许多主流 Linux 发行版不同。你可以通过 Alpine 的包管理器 apk 安装  chromium-chromedriver:

sudo apk add chromium-chromedriver

安装完后driver的路径在:/usr/lib/chromium/chromedriver

四,测试

python 代码测试

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

# 配置无头浏览器选项
options = Options()
options.add_argument("--headless")     # 使用无头模式,无 GUI的Linux服务器必须添加
options.add_argument("--disable-gpu")  # 不使用GPU,有的机器不支持GPU
options.add_argument("--no-sandbox")   # 运行 Chrome 的必需参数
options.add_argument("--disable-dev-shm-usage") 
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)

# chromedriver 的路径
driver_executable_path = "/usr/lib/chromium/chromedriver"

chrome_service = Service(driver_executable_path)
driver = webdriver.Chrome(service=chrome_service, options=options)


driver.get("http://www.baidu.com")

# 找到搜索框并输入关键词
search_box = driver.find_element(By.ID, 'kw')
search_box.send_keys('Python')

# 点击搜索按钮
search_button = driver.find_element(By.ID, 'su')
search_button.click()

# 等待页面加载
time.sleep(3)

# 获取搜索结果
results = driver.find_elements(By.CSS_SELECTOR, '.result.c-container')

# 提取标题和链接
for result in results:
    title = result.find_element(By.TAG_NAME, 'h3').text
    link = result.find_element(By.TAG_NAME, 'a').get_attribute('href')

    print(f'Title: {title}\nLink: {link}\n')

# 退出浏览器
driver.quit()

打印结果:

selenium模式
chrome_options.add_argument('--headless')            # 无头模式,可不启用界面显示运行
chrome_options.add_argument('--disable-gpu')        # 禁用GPU加速
chrome_options.add_argument('--start-maximized') #浏览器最大化
chrome_options.add_argument('--window-size=1280x1024') # 设置浏览器分辨率(窗口大小)
chrome_options.add_argument('log-level=3')
chrome_options.add_argument('--user-agent=""')       # 设置请求头的User-Agent
chrome_options.add_argument('--disable-infobars')   # 禁用浏览器正在被自动化程序控制的提示
chrome_options.add_argument('--incognito')              # 隐身模式(无痕模式)
chrome_options.add_argument('--hide-scrollbars')     # 隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('--disable-javascript')  # 禁用javascript
chrome_options.add_argument('--blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
chrome_options.add_argument('--ignore-certificate-errors')   # 禁用扩展插件并实现窗口最大化
chrome_options.add_argument('–disable-software-rasterizer')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--no-sandbox')  #以最高权限运行
chrome_options.add_argument('--disable-dev-shm-usage')

    ch_options = webdriver.ChromeOptions()
 
    # 不加载图片,加快访问速度
    ch_options.add_experimental_option("prefs", {"profile.mamaged_default_content_settings.images": 2})
 
    # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
    ch_options.add_experimental_option('excludeSwitches', ['enable-automation'])

    # ch_options.add_experimental_option("debuggerAddress", "127.0.0.1:9999")
    ch_options.add_argument('--proxy--server=127.0.0.1:8080')

    ch_options.add_argument('--disable-infobars')  # 禁用浏览器正在被自动化程序控制的提示
    ch_options.add_argument('--incognito')
    browser = webdriver.Chrome(options=ch_options)


http://www.kler.cn/news/361932.html

相关文章:

  • 江苏省医疗损害鉴定管理办法
  • 26备战秋招day11——基于CoNLL-2003的bert序列标注
  • 【算法系列-栈与队列】匹配消除系列
  • MySQL程序特别酷
  • 5. AOP
  • Sigrity 共模电感的S-parameter仿真数据导入
  • 自定义全局过滤器统计接口调用耗时
  • Anaconda 使用时conda出问题
  • 转变软件交付方式:通过统一 API 和测试策略提高质量和速度
  • Idea序列图插件-SequenceDiagram Core
  • FactoryBean接口的原理与使用场景
  • 【uniapp】使用Promise封装request
  • 大厂面试真题-了解云原生吗,简单说一下docker和k8s
  • electron-vite_10electron-updater软件更新
  • 【C语言】原码 反码 补码
  • 【知识科普】websocket深入了解
  • 糖果——差分约束 + 正环判定及其优化(手搓栈 + 标记法)
  • 搜维尔科技:Varjo XR-4 模拟驾驶
  • LeetCode 1750.删除字符串两端相同字符后的最短长度
  • Lattice_FPGA使用Synplify Pro进行综合
  • flv格式如何转换mp4?将flv转换成MP4格式的9种转换方法
  • 如何将 Elasticsearch 与流行的 Ruby 工具结合使用
  • Linux下进行用户的切换与创建以及细微设置
  • SIMPLOT: Enhancing Chart Question Answering by Distilling Essentials
  • LeetCode两数相加
  • ECharts饼图-饼图标签对齐,附视频讲解与代码下载