基于Python Selenium在Edge浏览器下爬取商品信息

#基于Python Selenium在Edge浏览器下爬取商品信息

问题背景描述

最近跑一个爬虫作业的时候用到了selenium,用的模板是Chrome浏览器的,而我自己用的是Edge,所以在一些代码上需要进行改动,否则会报错,比如Edge浏览器启动项,等等。这里记录一下,也给有同样需要的人提供参考。借鉴了一些其他文章,我会放在末尾参考资料里,代码我还没学会github,所以所有代码我都放文章里了,顺序为:主程序-定义的函数,我会先介绍主程序。

作业描述:给定一个商店网址,要求获取商店内5个最贵的商品名字和价格,使用工具为selenium,使用代码为python,实验环境为anaconda+jupyternotebook。
老师给的参考步骤如下,我没有严格按照步骤来写:

  1. Search the empty keyword(keyword = ‘’) to get every products
  2. fetch the pages with given keyword,这里keyword我们不输入,就会展示所有信息。
  3. There’re serval products on one page. To get the product detail, it requires you to access the product page by the ‘href url’
  4. get all the links of products on that page.
  5. Use a loop to get all the product pages.
  6. Parse the product name and price from that page.
  7. Store the product information
  8. Find the next_page url
  9. If next_page url exist then fetch the next_page and go back to Step (2)
  10. Sort the products by price and retrieve the names of top 5 as the result
  11. Save the top5 result as json file, name the file “result.json”.

同时也给了一些提示:

  1. It’s recommended to use a ‘dict’ to store the retrieving result (products name as key and price as value).所以我把原程序里面的’products‘,商品信息换成了dict格式,再排序,最后以字典输出前五个。
  2. All the above processes are run on your own local machine (jupyterhub is not working).
  3. A template code to parse the html pages is given on the Gitea.

工具准备

工具一:anaconda3+jupyter,这个不给教程了,也可以用其他环境。
工具二:需要下载Edge专用的驱动,网址为Edge Driver
下载完成后,放入python脚本同一目录下,我用的是jupyter,所以我放的如图:
驱动位置及命名

需要注意的是,下载完要改名字,改为:‘MicrosoftWebDriver.exe’,否则有可能出错。
工具二:当然最重要的就是你有一个代码可以跑爬虫,文末我放上我的完整代码供参考,只是一个例程,放在文章最后(现在还没放,等我做完实验再放)。

代码实现讲解以及代码改动说明

由于老师给的是Chrome的浏览器的例程,所以我们要进行修改,我这里用的版本是 95.0.1020.40 (官方内部版本) (64 位)。其实应该都差不多。

引用库

from selenium import webdriver
from selenium.webdriver.
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值