自动化爬虫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
相关推荐
素雪风华10 分钟前
只使用Docker+Maven实现全自动化流程部署服务;Docker创建ffmpeg环境;
java·运维·后端·docker·容器·自动化·maven
尼罗河女娲20 分钟前
【获取WebSocket】使用 Playwright 监听 Selenium 自动化测试中的 WebSocket 消息(一)
websocket·网络协议·selenium
薛不痒35 分钟前
网络爬虫下(一些对网页的操作)
爬虫
企微自动化37 分钟前
企业微信客户端 UI 自动化定位技术的稳定性和局限性
ui·自动化·企业微信
尼罗河女娲39 分钟前
【测试开发】Selenium + Chrome 自动化中常用 ChromeOptions 参数说明(实践总结)
chrome·selenium·自动化
小白学大数据39 分钟前
Python 爬虫如何分析并模拟 JS 动态请求
开发语言·javascript·爬虫·python
深蓝电商API1 小时前
2025爬虫技术前沿:AI驱动、多模态与反反爬的军备竞赛
人工智能·爬虫
古城小栈1 小时前
接口测试:Postman+Newman 自动化脚本实战指南
自动化·lua·postman
爱打代码的小林1 小时前
python(爬虫selenium)
爬虫·python·selenium
尼罗河女娲1 小时前
【获取WebSocket】使用 Playwright 监听 Selenium 自动化测试中的 WebSocket 消息(二)
websocket·网络协议·selenium