【发布时间】:2022-01-06 16:57:18
【问题描述】:
我想从不同的页面收集数据,但是当我获取数据并附加到 json 文件时,它在结构上很糟糕。我有一个字典num,我想将值中的所有数据附加为列表。例如
num:[1,2,3]
添加后
num:[1,2,3,4,5]
这是我的代码 -->
import requests
import json
from random import randint
from time import sleep
import os
num_list=[]
def req(i):
url = 'Website_link{}'.format(i)
x = requests.get(url, headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "ka",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/json",
"Host": "api2.myauto.ge",
"Origin": "https://www.myauto.ge",
"Pragma": "no-cache",
"Referer": "https://www.myauto.ge/",
"sec-ch-ua": "'Not A;Brand';v='99', 'Chromium';v='96', 'Google Chrome';v='96'",
"sec-ch-ua-platform": "Windows",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site"})
dict_to_json = json.loads(x.content.decode("utf-8"))
pretty_json = json.dumps(dict_to_json, indent=4)
return dict_to_json
def list_append(parse):
for i in parse["data"]["items"]:
num_list.append(str(i["client_phone"]))
def write_to_json():
num_dict={"num":num_list}
if os.stat("data.json").st_size == 0:
f = open("data.json", "w")
json.dump(num_dict, f, indent=2)
f.close()
else:
f = open("data.json","r+")
data=json.load(f)
f.close()
f = open("data.json", "a")
data["num"]+=([num_list])
json.dump(data,f,indent=2)
f.close()
for i in range(1,3):
sleep(randint(3, 5))
req_load=req(i)
list_append(req_load)
write_to_json()
num_list=[]
我得到这样的数据
"num": [
"995579448820",
"995595334438",
"995555154416",
"995577025245",
"995599485252",
"995597083412",
"995598919090",
"995599401516",
"995597092860",
"995551317007",
"995577063439",
"995514213366",
"995592088897",
"995577539231",
"995596940606"
]
}{
"num": [
"995579448820",
"995595334438",
"995555154416",
"995577025245",
"995599485252",
"995597083412",
"995598919090",
"995599401516",
"995597092860",
"995551317007",
"995577063439",
"995514213366",
"995592088897",
"995577539231",
"995596940606",
[
"995597032700",
"995595201008",
"995593620467",
"995555605884",
"995555183888",
"995597777422",
"995577125074",
"995595910006",
"995595910006",
"995577583288",
"995599037070",
"995558830303",
"995591085255",
"995597777608",
"995555788888"
]
]
}```
【问题讨论】:
标签: python json python-3.x parsing request