Python之selenium实现网页自动化和自动爬虫

2023-06-0922:05:25后端程序开发Comments1,482 views字数 1586阅读模式

举例某购物网站,通过selenium与python,实现主页上商品的搜索,并将信息爬虫保存至本地excel表内。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

一、python环境与selenium环境安装文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

python在官网下载并安装并且设置环境变量。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

selenium通过命令行下,pip install selenium进行安装,python与selenium都建议使用3.0版本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

这里使用chrome浏览器,下载chrome对应版本的webdrver驱动,将webdriver驱动放在python根目录下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

Python之selenium实现网页自动化和自动爬虫文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

chrome浏览器的版本通过这个方法查询:帮助-关于Google Chrome(G)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

Python之selenium实现网页自动化和自动爬虫文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

二、搜索商品后点击搜索按钮提交文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

这里需要用到selenium库、time库(其中sleep用于等待)以及xlwt(用于保存excel表)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 1 from selenium import  webdriver
 2 from time import sleep
 3 import xlwt
 4 #打开浏览器,这里用的chrome
 5 d=webdriver.Chrome()
 6 #设置窗口最大化
 7 d.maximize_window()
 8 #设置隐式等待
 9 d.implicitly_wait(30)
10 #打开网页
11 d.get("https://www.jd.com/")
12 #使用元素定位id找到搜索框
13 d.find_element_by_id("key").send_keys("洗发水")
14 #使用xpath定位到搜索按钮
15 d.find_element_by_xpath("/html/body/div[1]/div[4]/div/div[2]/div/div[2]/button").clic

Python之selenium实现网页自动化和自动爬虫文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

三、元素定位,找到对应信息,并保存文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

1 #初始化excel表文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 2 excel=xlwt.Workbook(encoding="utf-8")
 3 #增加sheet页
 4 sheet=excel.add_sheet("sheet1",cell_overwrite_ok=True)
 5 #定义第1行的内容,以及初始化num,用于从第二行开始写入对应的数据
 6 sheet.write(0,0,'序号')
 7 sheet.write(0,1,'商品')
 8 sheet.write(0,2,'价格')
 9 num=1
10 #通过元素定位面找到,在页面上找到对应商品的各个元素位置
11 goods=d.find_elements_by_xpath("/html/body/div[6]/div[2]/div[2]/div[1]/div/div[2]/ul/li")
12 sleep(5)
13 for good in goods:
14     #分别找到商品与价格所在的元素,并且取其中的文本信息,并去空行
15     price=good.find_element_by_xpath("div/div[3]/strong").text.replace("\n","-")
16     goodtext=good.find_element_by_xpath("div/div[4]/a/em").text.replace("\n","-")
17     sheet.write(num,0,num)
18     sheet.write(num,1,goodtext)
19     sheet.write(num,2,price)
20     num+=1
21     #print(goodtext,"|",price)
22 #保存至excel表
23 excel.save(r"C:\Users\Mr.White\Desktop\test001\jd.xls")
24 #页面退出
25 d.quit()

Python之selenium实现网页自动化和自动爬虫文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

四、结果预览与总结文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

Python之selenium实现网页自动化和自动爬虫文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

1、网页自动化过程中最主要的难度是元素定位,后面会专门去介绍一下元素定位文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

2、页面可能随着前端代码的变化,会使原本可以跑通的脚本失效,有一定维护成本,如何设计合理的自动化脚本就显得比较重要。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46645.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/46645.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定