Python使用selenium自动爬取苏宁易购商品数据

嗨喽~大家好呀,这里是魔王呐 ❤ ~!

python更多源码/资料/解答/教程等 点击此处跳转文末名片免费获取

环境介绍:

  • python 3.8

  • pycharm 专业版

  • selenium

  • 谷歌浏览器

  • 浏览器驱动

selenium:

人是怎么操作浏览器的 那么代码就怎么写

代码思路

  1. 开启一个浏览器 (谷歌)

  2. 输入链接地址 打开网页

  3. 提取数据

  4. 保存数据

代码展示

导入模块

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:926207505
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import csv

写表头

with open('suning.csv', mode='w', newline='', encoding='utf-8') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(['title', 'price', 'comment', 'store', 'href'])
  1. 开启一个浏览器 (谷歌)
driver = webdriver.Chrome()
  1. 输入链接地址 打开网页
driver.get('https://search.suning.com/%E7%94%B5%E8%A7%86/?safp=d488778a.homepagev8.gSearch.5&safc=hotkeyword.0.0&safpn=10001')
for page in range(20):

3.1 让页面往下滚动

    # document.querySelector("body > div.ng-footer > div.ng-s-footer").scrollIntoView()
    driver.execute_script('document.querySelector("body > div.ng-footer > div.ng-s-footer").scrollIntoView()')
    time.sleep(2)

3.2 提取数据

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:926207505
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
    items = driver.find_elements(By.CSS_SELECTOR, '.item-bg')
    for item in items:
        title = item.find_element(By.CSS_SELECTOR, '.title-selling-point').text
        price = item.find_element(By.CSS_SELECTOR, '.price-box').text
        comment = item.find_element(By.CSS_SELECTOR, '.evaluate-old.clearfix').text
        store = item.find_element(By.CSS_SELECTOR, '.store-stock').text
        href = item.find_element(By.CSS_SELECTOR, '.title-selling-point a').get_attribute('href')
        print(title, price, comment, store, href)
        # 写数据
        with open('suning.csv', mode='a', newline='', encoding='utf-8') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow([title, price, comment, store, href])
    # 点击下一页
    driver.execute_script('document.querySelector("#nextPage").click()')
# 阻塞
input()

尾语

最后感谢你观看我的文章呐~本次航班到这里就结束啦 🛬

希望本篇文章有对你带来帮助 🎉,有学习到一点知识~

躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。

最后,宣传一下呀~👇👇👇更多源码、资料、素材、解答、交流皆点击下方名片获取呀👇👇

相关推荐

  1. 运用selenium京东商品数据储存到MySQL数据库

    2023-12-28 13:44:05       13 阅读
  2. Python云平台

    2023-12-28 13:44:05       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-28 13:44:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-28 13:44:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-28 13:44:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-28 13:44:05       20 阅读

热门阅读

  1. 微信小程序电子菜单点菜系统模块代码分析

    2023-12-28 13:44:05       29 阅读
  2. #CCSP学习心得总结:2023年新收获CCSP云安全认证

    2023-12-28 13:44:05       36 阅读
  3. lsyncd + rsync实现文件实时同步

    2023-12-28 13:44:05       37 阅读
  4. WPF 已有资源字典文件,在xaml 里面引用

    2023-12-28 13:44:05       43 阅读
  5. vscode编译调试sln工程

    2023-12-28 13:44:05       45 阅读
  6. 发布订阅模式和观察者模式详解

    2023-12-28 13:44:05       36 阅读
  7. 什么是机密计算(Confidential Compute)?

    2023-12-28 13:44:05       44 阅读