自动化爬虫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
相关推荐
sanggou4 小时前
【Python爬虫】手把手教你从零开始写爬虫,小白也能轻松学会!(附完整源码)
开发语言·爬虫·python
全栈陈序员5 小时前
基于Rust 实现的豆瓣电影 Top250 爬虫项目
开发语言·爬虫·rust
小小测试开发6 小时前
JMeter高级用法全解析:从性能测试到自动化监控,解锁压测工具的隐藏实力
运维·jmeter·自动化
w***4818 小时前
Python中的简单爬虫
爬虫·python·信息可视化
q***9449 小时前
最新豆瓣电影Top250爬虫(附完整代码)
爬虫
Aruanjian88812 小时前
手动发货太慢?RPA批量处理TikTok订单,效率提升2500%[特殊字符]
自动化·跨境电商·电商·temu·自动化机器人·希音·小红书云帆
Splashtop高性能远程控制软件14 小时前
行业观察 | 微软修复63个漏洞,包含零日与CVSS 9.8关键漏洞
运维·安全·microsoft·自动化·远程桌面·splashtop
b***666117 小时前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
霍格沃兹测试开发学社测试人社区18 小时前
揭开帷幕:如何实现UI回归测试的全面自主化
人工智能·ui·自动化