自动化爬虫selenium

安装pip install Selenium==4.0

按照chrome版本下载好驱动

复制代码
# 下载驱动并使用
# https://registry.npmmirror.com/binary.html?path=chrome-for-testing/
# https://googlechromelab
python 复制代码
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

#加载驱动  按alt+enter快速导入模块
s=Service('index/chromedriver.exe')

#创建浏览器对象
web=webdriver.Chrome(service=s)
#全屏窗口展示
web.maximize_window()
#打开网页
web.get('https://www.baidu.com')
#print(web.page_source)
#使用lxml或者bs模块解析数据  selenium也提供了解析数据的方式

#先获取标签对象
#print(web.find_element(By.ID,'chat-textarea'))#返回标签对象    找不到则返异常
#web.find_elements()   返回列表    找不到返回空列表
#print(web.find_element(By.NAME,'textarea'))          比如说input name属性
#print(web.find_element(By.XPATH,'//textarea'))
#print(web.find_element(By.CLASS_NAME,'chat-input-textarea'))
#print(web.find_element(By.CSS_SELECTOR,'#chat-textarea'))

#在textarea标签中输入内容
textarea=web.find_element(By.ID,'chat-textarea')
textarea.send_keys('python')
#按回车
#textarea.send_keys(Keys.ENTER)

#点击百度一下
#获取百度一下按钮
button_tag=web.find_element(By.ID,'chat-submit-button')
#点击按钮
button_tag.click()
python 复制代码
s=Service('index/chromedriver.exe')

#创建浏览器对象
web=webdriver.Chrome(service=s)
#全屏窗口展示
web.maximize_window()
#打开网页
web.get('https://www.dangdang.com/')

#获取搜索框  往里面输入大模型
input=web.find_element(By.ID,'key_S')
input.send_keys('大模型')
#按回车
input.send_keys(Keys.ENTER)

#获取百度一下按钮
#button_tag=web.find_element(By.CLASS_NAME,'button')
#点击按钮
#button_tag.click()

lis=web.find_elements(By.CSS_SELECTOR,'#component_59 li')
#lis=web.find_elements(By.XPATH,'//ul[@id="component_59"]/li')
#print(len(lis))
count=0
for li in lis:
    #找到标题对象,获取属性title的值
    title=li.find_element(By.NAME,'itemlist-picture').get_attribute('title')

    price=li.find_element(By.CLASS_NAME,'search_now_price').text
    try:
       zuoze=li.find_element(By.NAME,'itemlist-author').get_attribute('title')
    except NoSuchElementException:
       zuoze=''
    desc=li.find_element(By.CLASS_NAME,'detail').text

    date=li.find_element(By.XPATH,'.//P[@CLASS="search_book_author"]/span[2]').text.replace('/','')
    try:
        cbs = li.find_element(By.NAME, 'P_cbs').text
    except Exception as e:
        cbs=''

    print(count,title,price,zuoze,cbs,desc,date)
    count+=1
相关推荐
测试者家园15 小时前
从需求文档到测试点:利用大模型实现需求理解的自动化
软件测试·自动化·llm·需求分析·持续测试·智能化测试·功能点
GISer_Jing15 小时前
阿里开源纯前端浏览器自动化 PageAgent,[特殊字符] 浏览器自动化变天啦?
前端·人工智能·自动化·aigc·交互
vx_biyesheji000117 小时前
计算机毕业设计:Python多源新闻数据智能舆情挖掘平台 Flask框架 爬虫 SnowNLP ARIMA 可视化 数据分析 大数据(建议收藏)✅
爬虫·python·机器学习·数据分析·django·flask·课程设计
紫丁香18 小时前
Selenium自动化测试详解1
python·selenium·测试工具·ui
Blurpath住宅代理18 小时前
AI代理配置实战指南:构建高可用、低风险的网络出口层
人工智能·ai·自动化·静态ip·动态代理·住宅ip·住宅代理
小陈的进阶之路18 小时前
Selenium 常用操作 API
python·自动化·pytest
j_xxx404_19 小时前
爬虫对抗:ZLibrary反爬机制实战分析 (三) - 突破高频访问限制与TLS指纹(JA3)风控
爬虫
16Miku19 小时前
Mapping-Skill:把 AI/ML 人才搜索、作者挖掘与个性化触达整合成一条工作流
爬虫·ai·飞书·agent·skill·openclaw·龙虾
j_xxx404_19 小时前
爬虫对抗:ZLibrary反爬机制实战分析 (一) - 撕裂前端JS混淆与环境检测伪装
爬虫
twc82920 小时前
打造专属 MCP Server 测试自动化的私有化解决方案
运维·软件测试·人工智能·自动化·mcp server