自动化爬虫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
相关推荐
喵手1 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
喵手3 小时前
Python爬虫实战:构建各地统计局数据发布板块的自动化索引爬虫(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集数据csv导出·采集各地统计局数据发布数据·统计局数据采集
深蓝电商API4 小时前
滑块验证码破解思路与常见绕过方法
爬虫·python
sensen_kiss5 小时前
INT303 Coursework1 爬取影视网站数据(如何爬虫网站数据)
爬虫·python·学习
生而为虫7 小时前
Selenium打开网页时保持登陆状态
selenium·测试工具
小小张说故事8 小时前
BeautifulSoup:Python网页解析的优雅利器
后端·爬虫·python
Yeats_Liao8 小时前
评估体系构建:基于自动化指标与人工打分的双重验证
运维·人工智能·深度学习·算法·机器学习·自动化
一晌小贪欢8 小时前
Python 爬虫进阶:如何利用反射机制破解常见反爬策略
开发语言·爬虫·python·python爬虫·数据爬虫·爬虫python
好好学习天天向上~~9 小时前
6_Linux学习总结_自动化构建
linux·学习·自动化
深蓝电商API10 小时前
爬虫请求频率控制与模拟人类行为
爬虫