小言_互联网的博客

Python爬虫系列之美团优选商家端商品自动化管理(商品发布、商品排期、订单采集)

534人阅读  评论(0)

Python爬虫系列之美团优选商家端商品自动化管理(商品发布、商品排期、订单采集)

小程序爬虫接单、app爬虫接单、网页爬虫接单、接口定制、网站开发、小程序开发> 点击这里联系我们 <

微信请扫描下方二维码

代码仅供学习交流,请勿用于非法用途

直接上代码

# -*- coding:utf-8 -*-
import requests
import datetime
import time
import json
import os
import xlrd
import xlwt
from xlutils.copy import copy

'''
	功能点:
		1、美团优选商家端商品自动上架
		2、美团优选商家端商品自动排期
		3、美团优选商家端商品订单自动采集
		wx:walei5201314
'''

retry = 3
timeout = 20
excelTitle = ["productId", "skuId", "商品名称", "销售区域", "供货价", "库存", "销售单位", "销售日期", "生产日期", "市场价", "坑位"]
excelPwd = os.getcwd() + "/paiqi/"
if not os.path.exists(excelPwd):
    os.mkdir(excelPwd)
cookie = ""
mallPassportToken = ""


def initExcel(path):
    try:
        f = xlwt.Workbook()
        sheet1 = f.add_sheet(u'double', cell_overwrite_ok=True)
        for i in range(0, len(excelTitle)):
            sheet1.write(0, i, excelTitle[i])
        f.save(path)
        return True
    except Exception as e:
        return False


def writeExcel(data, path):
    print("===========================================================")
    print(data)
    print("===========================================================")
    try:
        workbook = xlrd.open_workbook(path)
        sheets = workbook.sheet_names()
        worksheet = workbook.sheet_by_name(sheets[0])
        rows_old = worksheet.nrows
        new_workbook = copy(workbook)
        new_worksheet = new_workbook.get_sheet(0)
        for j in range(0, len(data)):
            try:
                new_worksheet.write(rows_old, j, str(data[j]))
            except Exception as e:
                continue
        new_workbook.save(path)
        return True
    except Exception as e:
        pass
    return False


def dateTots(s):
    try:
        return int(time.mktime(time.strptime(s, "%Y/%m/%d"))) * 1000
    except Exception as e:
        return None


def getHtml(url, headers):
    for i in range(retry):
        try:
            resp = requests.get(url, headers=headers, timeout=timeout)
            return json.loads(resp.content.decode("utf-8"))
        except Exception as e:
            pass


def postHtml(url, data, headers):
    for i in range(retry):
        try:
            resp = requests.post(url, headers=headers, data=data, timeout=timeout)
            return json.loads(resp.content.decode("utf-8"))
        except Exception as e:
            pass


def updateCookie():
    global cookie
    global mallPassportToken
    try:
        with open("cookie.txt", "r", encoding="utf-8") as f:
            fs = f.readlines()
            cookie = fs[0]
    except Exception as e:
        print("启动错误:程序所在目录未读取到 cookie.txt 文件!")
        exit(0)


def mkdir(path):
    try:
        if os.path.exists(path):
            return True
        os.mkdir(path)
        return True
    except Exception as e:
        pass
    return False


def getCurrentTime():
    return str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))


def getCurrentDate():
    return str(time.strftime('%Y-%m-%d', time.localtime(time.time())))


def getSechduleRegionList():
    url = "https://vss-grocery.meituan.com/api/vss/shepherd/schedule/VendorSkuScheduleQueryApiService/searchVendorHistoryScheduleRegion?version="
    headers = {
   
        "mall-passport-token": mallPassportToken,
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.2(0x18000231) NetType/4G Language/zh_CN",
        "mall-login-type": "passport-temp",
        "Referer": "https://servicewechat.com/sssssss5521212/148/page-frame.html",
        "Cookie": cookie
    }
    res = postHtml(url, {
   }, headers)
    try:
        return res['data']
    except Exception as e:
        pass


def tsToDate(ts):
    if ts:
        timeArray = time.localtime(int(ts))
        return str(time.strftime("%Y-%m-%d", timeArray))
    return ""


def getSellPosition(sellPosition):
    if sellPosition is None or sellPosition == "null":
        return ""
    elif str(sellPosition) == "4":
        return "万人团"
    elif str(sellPosition) == "3":
        return "秒杀"
    elif str(sellPosition) == "2":
        return "爆品"
    elif str(sellPosition) == "1":
        return "常规"
    return ""


def schduleParser(schduleStartTs, schduleEndTs, regionId, regionName, excelPath):
    url = "https://vss-grocery.meituan.com/api/vss/shepherd/schedule/VendorSkuScheduleQueryApiService/searchScheduleList?version="
    page = 1
    while True:
        data = {
   
            "paging": {
   
                "offset": (page - 1) * 10,
                "limit": 10
            },
            "skuName": "",
            "scheduleStatus": 0,
            "saleStartTime": schduleStartTs,
            "saleEndTime": schduleEndTs
        }
        headers = {
   
            "mall-passport-token": mallPassportToken,
            "Content-Type": "application/json",
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.2(0x18000231) NetType/4G Language/zh_CN",
            "mall-login-type": "passport-temp",
            "Referer": "https://servicewechat.com/wx40f6ce973eb82d6b/148/page-frame.html",
            "Cookie": cookie
        }
        res = postHtml(url, json.dumps(data), headers)
        try:
            scheduleList = res['data']['scheduleList']
            if scheduleList and len(scheduleList) > 0:
                for schedule in scheduleList:
                    try:
                        datas = []
                        datas.append(schedule['productId'])
                        datas.append(schedule['skuId'])
                        datas.append(schedule['name'])
                        datas.append(schedule['stock'])
                        datas.append(schedule['unitName'])
                        datas.append(tsToDate(str(schedule['saleDate'])[: 10]))
                        try:
                            datas.append(tsToDate(str(schedule['productProductionDate'])[: 10]) if schedule['productProductionDate'] else "")
                        except Exception as e:
                            datas.append("")
                        try:
                            datas.append(str(schedule['cutlinePrice']) if schedule['cutlinePrice'] else "")
                        except Exception as e:
                            datas.append("")
                        try:
                            datas.append(getSellPosition(str(schedule['sellPosition'])))
                        except Exception as e:
                            datas.append("")
                        writeExcel(datas, excelPath)
                    except Exception as e:
                        pass
                total = int(res['data']['total'])
                if total < 10:
                    break
                page += 1
            else:
                break
        except Exception as e:
            pass
    return False


def getBeforeDaysDate(days):
    threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days=days))
    return dateTots(str(threeDayAgo.strftime('%Y/%m/%d')))


def main():
    updateCookie()
    excelPath = excelPwd + getCurrentDate() + ".xls"
    if status:
        startTs = getBeforeDaysDate(0)
        sechduleRegionList = getSechduleRegionList()
        if sechduleRegionList and len(sechduleRegionList) > 0:
            for sechduleRegion in sechduleRegionList:
                try:
                    schduleParser(startTs, endTs, str(sechduleRegion['regionId']), sechduleRegion['regionName'], excelPath)
                except Exception as e:
                    pass
        else:
            print("获取销售地区失败,请检查是否登录过期!")
    else:
        print("excel:%s 创建失败!请检查excel是否处于打开状态,如果是请关闭!" % excelPath)


if __name__ == '__main__':
    main()


转载:https://blog.csdn.net/qq_41287993/article/details/114417145
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场