Last active
July 9, 2021 06:52
-
-
Save hooklife/b416c326e1ea726b38003f44b9109ed0 to your computer and use it in GitHub Desktop.
饿了么满减
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import pymysql | |
import logging | |
geohash = 'wxrvbuh1qcb' | |
latitude = '41.81419' | |
longitude = '123.43718' | |
offset = 0 | |
limit = 50 | |
db = pymysql.connect("127.0.0.1", "root", "root", "eleme", charset='utf8') | |
logging.basicConfig( | |
level=logging.DEBUG, | |
format="[%(asctime)s] %(name)s:%(levelname)s: %(message)s" | |
) | |
for index in range(500): | |
url = "https://www.ele.me/restapi/shopping/restaurants?extras%5B%5D=activities&geohash=" + str(geohash) + \ | |
"&latitude=" + str(latitude) + "&longitude=" + str(longitude) + "&offset=" + str(offset) + \ | |
"&limit=" + str(limit) + "&terminal=web" | |
cookies = dict(SID='*', USERID='*', | |
ubt_ssid="*" | |
) | |
data = requests.get(url, cookies=cookies).json() | |
logging.info("offset:" + str(offset)) | |
logging.debug(data) | |
if len(data) <= 0: | |
logging.info("end") | |
sys.exit(0) | |
offset += limit | |
for restaurant in data: | |
for activitie in restaurant['activities']: | |
if "type" in activitie and activitie['type'] is 102: | |
attribute = json.loads(activitie["attribute"]) | |
for manjian in attribute: | |
name = restaurant["name"] | |
tips = activitie["tips"] | |
buy = manjian | |
discount = attribute[manjian]["1"] | |
cursor = db.cursor() | |
# SQL 插入语句 | |
sql = "INSERT INTO activities(name, \ | |
tips, buy, discount) \ | |
VALUES ('%s', '%s', '%s', '%s')" % \ | |
(name, tips, buy, discount) | |
# 执行sql语句 | |
cursor.execute(sql) | |
# 提交到数据库执行 | |
db.commit() | |
db.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment