双十一了,我用PYTHON获取今年双十一商品,并保存成excel表格数据,看看值不值

随着十一的来临,我们的钱包又要一次变空了。
尤其还是有女朋友的同志们~
心疼钱包~
好吧,废话不多讲

这次我们需要的环境

  • python 3.8
  • pycharm

需要的模块

  • selenium
  • csv
  • time
  • random
    不会安装的同志们,在cmd里输入pip install +模块名 。
    就可以安装好了

对了最重要的 ,不要忘记安装谷歌驱动
不会安装谷歌驱动或者模块没安装好的,可以私信我!

效果如下:

在这里插入图片描述

直接上代码

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="f-login-id"]').send_keys(TAO_USERNAME1)
    time.sleep(random.randint(1, 3))  # 尽量避免人机检测  随机延迟

    driver.find_element_by_xpath('//*[@id="f-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-clearfx"]/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()

# 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))  # 尽量避免人机检测  随机延迟

最后

友情提醒,切勿多次尝试,容易被反爬
更多推荐:
Python基础入门教程推荐

Python爬虫案例教程推荐

猜你喜欢

转载自blog.csdn.net/sinat_38682860/article/details/121171354
今日推荐