python五步教你如何浏览器自动化并保存商品数据信息~

亮点:

1、系统分析目标网页

2、html标签数据解析方法

3、海量数据一键保存

环境介绍:

  • python 3.8
  • pycharm 2021专业版
  • selenium >>> pip install selenium==3.141.0

代码

导入模块

from selenium import webdriver
import time
import csv

1. 打开谷歌浏览器

driver = webdriver.Chrome()

for page in range(0, 38):
    print(f'------------------------正在爬取第{page+1}页------------------------')

2. 打开网页

    driver.get(f'https://search.suning.com/%E4%B8%9D%E8%A2%9C%E6%83%85%E8%B6%A3/&iy=0&isnoresult=0&cp={page}')
    # 灵活等待
    driver.implicitly_wait(1)
    drop_down()
    get_next()

3. 获取商品数据

    # 网页 html: <div class="">茅台1 <div>茅台2 <div>茅台3</div></div></div>   <div>茅台2</div>   <img src="图片"/>
    # <a href="网页链接"></a>:
    divs = driver.find_elements_by_css_selector('.item-bg')

所有的商品都拿出来

    for div in divs:

标题

        title = div.find_element_by_css_selector('.title-selling-point a').text

价格

        price = div.find_element_by_css_selector('.def-price').text

评论

        try:
            comment_num = div.find_element_by_css_selector('.info-evaluate a').text
        except:
            comment_num = 0

商铺名字

        store_name = div.find_element_by_css_selector('.store-stock a').text

详情页链接

get_attribute: 获取标签属性

       detail_url = div.find_element_by_css_selector('.title-selling-point a').get_attribute('href')
        print(title, price, comment_num, store_name, detail_url)

4. 保存数据

        # a: 追加写入
        # wb: 以二进制方式覆盖写入
        with open('苏宁.csv', mode='a', encoding='utf-8', newline='') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow([title, detail_url])

with open('苏宁.csv', newline='') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(['title', 'price', 'comment_num', 'store_name', 'detail_url'])

5. 翻页保存

def drop_down():
    """执行页面滚动的操作"""
    for x in range(1, 12, 2):
        time.sleep(1)
        j = x / 9
        js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
        driver.execute_script(js)

def get_next():

尾语

好了,我的这篇文章写到这里就结束啦!

有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...