Files
xiaoxiang/小象超市详情补采部分.py
2026-04-15 18:12:30 +08:00

215 lines
6.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os.path
import random
import time
import datetime
import pymysql
import requests
import json
from newsetting import All_Setting
tent2 = All_Setting(host='183.6.7.91', port=33016, db='tk_test')
tent3 = All_Setting(host='183.6.7.91', port=33016, db='headers_project')
def get_sign_new(url, data, dfpid, mtg_origin):
headers = {"content-type": "application/json", "token": "jksdfhiownefopwef902fsdf"}
body = {
"mtg_origin": mtg_origin,
"url": url,
"data": data,
"dfpid": dfpid,
"method": "POST"}
# url = "http://124.222.120.219:18003/hz_wm_jym_1008"
url = 'http://8.138.127.139:10063/newmtgsig'
req = requests.post(url, headers=headers, json=body, proxies={'http': None, 'https': None},
verify=False).text
return req
def get_Datatext(dataq):
str1 = ''
for i in dataq:
key, value = i, dataq[i]
str2 = f'{key}={value}&'
str1 = str1 + str2
data = str1[0:-1]
return data
def get_hd():
sql3 = f"""
select * FROM elephant_hd where is_ok is null limit 1
"""
da = tent3.get_shoppingmalls(sql3)
return da
def return_id1(keyword1, keyword2):
import json
with open('all', 'r', encoding='utf-8') as f:
data = json.load(f)
alllist = []
for i in data["data"]["poiCategories"]: # 一级标签
onelist = i.get('subPoiCategories', [])
two_list = []
if onelist == []:
continue
else:
for y in onelist:
two_list.append(
{
'id': y['id'],
'name': y['name'],
}
)
alllist.append(
{
'id': i['id'],
'name': i['name'],
'twolist': two_list
}
)
# print(alllist)
for k in alllist:
if k['name'] == keyword1:
id1 = k['id']
for y in k['twolist']:
if y['name'] == keyword2:
id2 = y['id']
return (id1, id2)
else:
continue
else:
continue
def next_or(response):
if response.json()['data']['pageInfo']['nextPageOffset'] != -1:
return True
else:
return False
# 单次访问
def one_(keywordid1, keywordid2, num, shop_id, data):
import re
id_list = [int(item[0]) for item in data]
hd_data = json.loads(get_hd()[0][2])
headers = hd_data[0]
url = hd_data[1]
query = url.split('?', 1)[1]
params = dict(re.findall(r'([^&=]+)=([^&]*)', query))
data = json.loads(hd_data[2])
params.update({'poi': str(shop_id)})
params.update({'stockPois': str(shop_id)})
url = f"https://mall.meituan.com/api/c/poi/{str(shop_id)}/sku/list/category/v8?"
url1 = url + get_Datatext(params)
id1, id2 = keywordid1, keywordid2
data.update({'firstCategoryId': id2})
data.update({'poiId': shop_id})
data.update({'categoryId': id2})
data.update({'listTag': 0})
data['pageInfo'].update({'offset': (num - 1) * 20})
data['pageInfo'].update({'nextPageOffset': (num - 1) * 20})
data['secondCategoryInfo'].update({'secondCategoryId': id2})
data['secondCategoryInfo'].update({'type': 1})
data['displayedCardMap'] = {'1': {'cardIds': id_list}}
try:
del data['thirdCategoryInfo']
del data['pageInfo']['currentPage']
del data['pageInfo']['pageSize']
del data['pageInfo']['curPageOffset']
del data['pageInfo']['lastPageOffset']
del data['pageInfo']['firstPageSize']
except:
pass
data = json.dumps(data, separators=(',', ':'))
mtg_origin = eval(headers['mtgsig'])
dfpid = mtg_origin['a3']
headers['mtgsig'] = get_sign_new(url1, data, dfpid, mtg_origin)
response = requests.post(url1, headers=headers, data=data, verify=False)
print(response.text)
return response, shop_id
# 每个分类都跑一次拿总数(自带20)
def all_one(shop_id, one_id, format_today):
num = 1
# 分类限制
sql3 = f"""
select two_id,one_all_num,shop_id FROM category_detail where one_id='{one_id}'and shop_id='{shop_id}' and work_id ='{format_today}' AND one_all_num IS NOT NULL
"""
# 无限制
# sql3 = f"""
# select * FROM category_detail where all_num = 0 and is_ok =0 and now_num = 0
# """
da = tent2.get_shoppingmalls(sql3)
print(da)
for i in da:
two_id = i[0]
one_all_num = i[1]
sql = f"""
SELECT DISTINCT goods_id
FROM `tk_test`.`data_completed`
WHERE `work_id` = '{format_today}'
AND `s_id` = '{two_id}'
AND `f_id` LIKE '{one_id}'
"""
data = tent2.get_shoppingmalls(sql)
print(data)
if len(data) < one_all_num:
req,shop_id = one_(i[2], i[0], num, shop_id, list(data))
try:
req = req.json()
except:
print('号有问题')
print(req)
if req['data'] == {}:
print('账号没访问到' * 20)
print('下一个账号试试' * 20)
# tent2.write_data_one(
# """
# INSERT INTO category_detail_log
# (shop_id,category_detail_id, one_id, two_id, req_json,work_id)
# VALUES (%s,%s, %s, %s, %s,%s)
# """,
# (shop_id, num, i[2], i[4], json.dumps(req, ensure_ascii=False),format_today)
# )
# tent2.update_data(update_sql)
continue
tent2.write_data_one(
"""
INSERT INTO category_detail_log
(shop_id,category_detail_id, one_id, two_id, req_json,work_id)
VALUES (%s,%s, %s, %s, %s,%s)
""",
(shop_id, num, one_id, two_id, json.dumps(req, ensure_ascii=False), format_today)
)
format_today = str(datetime.date.today()).replace("-", "") + 'tk'
shop_id = 10000900
# 40581
sql3 = f"""
select DISTINCT one_id FROM category_detail where work_id ='{format_today}' AND one_all_num IS NOT NULL
"""
da = tent2.get_shoppingmalls(sql3)
id_list = [int(item[0]) for item in da]
one_ids = id_list
print(one_ids)
for one_id in one_ids:
all_one(shop_id, one_id, format_today)
#
# while 1:
# if one_all(shop_id,format_today)==0:
# print(728)
# # time.sleep(random.randint(3,15))
# else:break
# input()