自动化爬虫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
相关推荐
缺点内向6 分钟前
C#: 如何自动化创建Word可填写表单,告别手动填写时代
c#·自动化·word
元媛媛10 分钟前
UiPath |5个基础自动化场景
android·java·自动化
袁袁袁袁满24 分钟前
Python爬虫下载PDF文件
爬虫·python·pdf·python爬虫下载pdf文件
2501_9419820535 分钟前
行业实践:如何利用 RPA 自动化技术提升企业微信外部群的运营效率?
自动化·企业微信·rpa
宇钶宇夕40 分钟前
CoDeSys入门实战一起学习(五):CoDeSys V3 车库门控制编程全解析系列(手册基础第二篇)
运维·自动化·软件工程
霍格沃兹软件测试开发44 分钟前
大语言模型提示词工程教程:实现自动化工具推理
microsoft·语言模型·自动化
梦想的旅途21 小时前
利用 RPA 自动化技术实现企业微信外部群的功能扩展与链路闭环
自动化·企业微信·rpa
天空属于哈夫克31 小时前
非官方接口实现企业微信外部群主动调用:基于RPA的自动化实践
自动化·企业微信·rpa
汽车仪器仪表相关领域14 小时前
全自动化精准检测,赋能高效年检——NHD-6108全自动远、近光检测仪项目实战分享
大数据·人工智能·功能测试·算法·安全·自动化·压力测试
骆驼爱记录14 小时前
Excel高效粘贴技巧:仅填充可见单元格
自动化·excel·wps·新人首发