【python】教你使用seLeniun爬取淘宝商品数据(内含完整源码)

19 篇文章 4 订阅

前言

嗨喽~大家好,这里是魔王,希望和大家一起努力进步,觉得文章还行你就三连支持一下叭ˋ( ° ▽、° )

[课 题]:

使用python采集某宝商品数据

[课题介绍]:

随着互联网时代的到来,人们更加倾向于互联网购物

某宝又是电商行业的巨头,在某宝平台中有很多商家数据

今天带大家使用python+selenium工具获取这些公开的商家数据
在这里插入图片描述

[环境介绍]:

  • python 3.8
  • pycharm
  • selenium
  • csv
  • time
  • random
  • python解释器安装包、安装教程
  • pycharm代码编辑器安装包、安装教程、激活码
  • 私我领取
    1.编程小白的第一本Python入门书.pdf
    2.Chrome浏览器 Webdriver 插件安装教程 【课堂素材必备】
    3.xpath-helper插件安装教程【课堂素材必备】|

Python基础 + Python进阶 + 爬虫 + Python数据分析 + Python全栈课程

爬虫工程师: 学习爬虫技术, 抓取互联网中能够看到的数据  python + selenium
数据分析师: 基于数据做系统分析, 挖掘数据价值
全栈开发工程师: 网站服务

请添加图片描述

# 下载操作浏览器驱动的第三方模块  selenium  pip install selenium

from selenium import webdriver
import time  # 时间模块, 可以用于程序的延迟
import random  # 随机数模块
from constants import TAO_USERNAME1, TAO_PASSWORD1
import csv  # 数据保存的模块

搜索商品数据, 登录用户

def search_product(keyword):
    driver.find_element_by_xpath('//*[@id="q"]').send_keys(keyword)
    time.sleep(random.randint(1, 3))  # 尽量避免人机检测  随机延迟

    driver.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button').click()
    time.sleep(random.randint(1, 3))  # 尽量避免人机检测  随机延迟
# 登录
driver.find_element_by_xpath('//*[@id="fm-login-id"]').send_keys(TAO_USERNAME1)
time.sleep(random.randint(1, 3))  # 尽量避免人机检测  随机延迟

driver.find_element_by_xpath('//*[@id="fm-login-password"]').send_keys(TAO_PASSWORD1)
time.sleep(random.randint(1, 3))  # 尽量避免人机检测  随机延迟

driver.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click()
time.sleep(random.randint(1, 3))  # 尽量避免人机检测  随机延迟

解析商品数据

def parse_data():
    divs = driver.find_elements_by_xpath('//div[@class="grid g-clearfix"]/div/div')  #  所有的div标签

    for div in divs:
        try:
            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/span[2]').text
            location = div.find_element_by_xpath('.//div[@class="location"]').text
            detail_url = div.find_element_by_xpath('.//div[@class="pic"]/a').get_attribute('href')

            print(info, price, deal, name, location, detail_url)
# 保存
            with open('某宝.csv', mode='a', encoding='utf-8', newline='') as f:
                csv_write = csv.writer(f)
                csv_write.writerow([info, price, deal, name, location, detail_url])
        except:
            continue

创建一个浏览器

word = input('请输入你要搜索商品的关键字:')
driver = webdriver.Chrome(executable_path='../chromedriver.exe')

# selenium操作的浏览器被识别了, 无法登录
# 修改浏览器的部分属性, 绕过检测
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",
            {"source": """Object.defineProperty(navigator, 'webdriver', {get: () => false})"""})
# 执行自动化浏览器的操作
driver.get('https://www.taobao.com/')
driver.implicitly_wait(10)  # 设置浏览器的等待,加载数据
driver.maximize_window()  # 最大化浏览器
# 调用商品搜索的函数
search_product(word)

for page in range(100): # 012
    print(f'\n==================正在抓取第{page + 1}页数据====================')
    url = f'https://s.taobao.com/search?q=%E5%B7%B4%E9%BB%8E%E4%B8%96%E5%AE%B6&s={page * 44}'
    # 解析商品数据
    parse_data()
    time.sleep(random.randint(1, 3))  # 尽量避免人机检测  随机延迟
  • 4
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值