我想从文件中写入vehiclelist
,而不是一个一个地写入。例如,我想从文件中读取1000 vehicleid
。
import sqlalchemy as sa
import psycopg2
import requests as rq
import pandas as pd
import json
import time
start_time = time.time()
while True:
temp = pd.DataFrame()
df = pd.DataFrame()
**vehicleList = {"LTZ1246", "SN63NBK", "LX12DJY", "YX16OKV", "YX68UTM", "LX59EDK", "YY18TLX", "SO68HDX", "BL65OYK","YY67UTP", "YY18TKO", "SN16OKV", "LX59ECJ", "LX12DCY", "YY18TKO", "LX12DGE", "LX58CGE", "BF15KHB", "SN14TXR", "SN67XCB"}**
for ids in vehicleList:
r = rq.get('https://api.tfl.gov.uk/Vehicle/' + ids + '/Arrivals')
r = r.text
temp = pd.read_json(r)
temp['Type'] = 'ids'
df = pd.concat([df, temp], sort=False).reset_index(drop=True)
engine = sa.create_engine('postgresql+psycopg2://postgres:3434@127.0.0.1/postgres')
df['timing'] = list(map(lambda x: json.dumps(x), df['timing']))
df.to_sql('tfl_bus_pg2', engine, if_exists='append', index=False)
time.sleep(60)