社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python自动化爬取淘宝商品数据导入execl表格

夜斗小神社 • 3 年前 • 438 次点击  

hello,大家好,我是夜斗小神社!

电商时代,淘宝、京东、天猫商品数据对店铺运营有极大的帮助,因此获取相应店铺商品的数据能够带来极大的价值,那么我们如何获取到相应的数据呢?

上一篇我们讲了python打包exe可执行文件:
Python打包成exe文件史上最详细教程:
https://blog.csdn.net/xtreallydance/article/details/112643658
这次我们来讲解淘宝爬虫代码————代码如下:

from selenium import webdriver
import time
import csv
import re
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

导入自动化的库selenium, 缓存时间time库, 将文件保存为csv形式, 导入re正则匹配的库

if __name__ == '__main__':
    keyword = input("请输入你要商品的关键字:")
    path = r'L:\webdriver\chromedriver.exe'
    driver = webdriver.Chrome(path)
    driver.get('https://www.taobao.com/')
    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输入查询的关键字: 例如输入ins潮流T恤,path为webdriver.exe驱动设备存放的路径,实例化一个driver对象,然后利用get方法访问淘宝网址!再调用main()方法.

请大家记住,一定要扫码登录!不然会被淘宝反爬!如图所示!

在这里插入图片描述

  • 程序运行开始结果如下:

在这里插入图片描述

def main():
    print('正在爬取第一页数据')

    page = search_product(keyword)
    get_product()
    page_num = 1
    # q不变 0 44 188(页数*44)
    while page_num != page:
        print('-*-' * 50 )
        print('正在爬取第{}页的数据'.format(page_num + 1))
        print('*-*' * 50)
        driver.get('https://s.taobao.com/search?q={}&s={}'.format(keyword, page_num))
        # 浏览器等待方法
        driver.implicitly_wait(2)
        # 最大化浏览器
        driver.maximize_window()
        get_product()
        page_num += 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

main()方法当中呢,首先利用search_product函数与get_product函数爬取一页的数据, 再利用while循环爬取全部数据, 我们首先讲解一页数据的爬取.

def search_product(key):
    driver.find_element_by_id


    
('q').send_keys(key)
    driver.find_element_by_class_name('btn-search').click()
    # 浏览器窗口最大化
    driver.maximize_window()
    time.sleep(15)
    # 因为自动登录基本实现不了,所以要实现手动登录
    # 找到页数的标签
    page = driver.find_element_by_xpath('//*[@id="mainsrp-pager"]/div/div/div/div[1]').text
    page = re.findall('(\d+)', page)[0]
    return int(page)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

首先利用driver.find_element_by_id这个方法找到输入框,将key这个变量输入到搜索框中,再利用driver.find_element_by_class_name方法找到搜索两个字,利用click()方法点击搜索.将窗口最大化,并暂停15s.因为自动登录淘宝当中会被阿里巴巴识别,所有暂停15s是为了用手动扫码登录. 然后用用xapth找到page页数的标签,匹配数字获取第一个值,返回page页数,比如第5页,返回的就是5,将参数传入到page中,在调用get_product()方法获取这一页的商品详细数据,例如商品名字、商品价格、付款人数、商品地址、商品店铺名等等等,下面来看get_product()这个函数

在这里插入图片描述

def get_product():
    divs = driver.find_elements_by_xpath('//div[@class="items"]/div[@class="item J_MouserOnverReq  "]')
    print(divs)
    for div in divs:
        # 商品名称
        info = div.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text
        # 商品价格
        price = div.find_element_by_xpath('.//strong').text + "元"
        # 付款人数
        deal = div.find_element_by_xpath('.//div[@class="deal-cnt"]').text
        # 店铺名称
        name = div.find_element_by_xpath('.//div[@class="shop"]/a').text
        # 店铺地点
        place = div.find_element_by_xpath('.//div[@class="location"]').text
        print(info, price, deal, name, place, sep='|')
        with open('ins短袖.csv', 'a', newline="") as fp:
            csvwriter = csv.writer(fp, delimiter=',')
            csvwriter.writerow([info, price, deal, name, place])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

首先找到商品列表的标签divs,然后利用for循环获取每一个商品的div标签,利用xpath语法获取到info、price、deal、name、place信息然后保存为csv文件形式!

  • 最终爬取下来的数据导入到excel中,如图所示:
    在这里插入图片描述

好啦今天的分享就到这里啦,然后小夜斗就要滚去学习啦,see you!

  • 在这个星球上,你很重要,请珍惜你的珍贵! ~~~夜斗小神社

在这里插入图片描述

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/106319
 
438 次点击