I am trying to save incoming data from d in postgres database but it keeps returning listdata has no attribute data and I don't understand why - if I'm not mistaken, there is only one column that needs to be saved right?
If you do print(d) you can see the data coming but I don't know how to save it.
from datetime import datetime
from requests import Session
import pandas as pd
import matplotlib.pyplot as plt
import psycopg2
import time
from psycopg2.extras import RealDictCursor
page_url = "https://www.nseindia.com/get-quotes/equity?symbol=LT"
chart_data_url = "https://www.nseindia.com/api/chart-databyindex"
s= Session()
h = {"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36",
"accept": "text/html,application/xhtml xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
}
s.headers.update(h)
r = s.get(page_url)
def fetch_data(symbol):
data = {"index": symbol "EQN"}
r = s.get(chart_data_url, params=data)
data = r.json()['grapthData']
return [[datetime.utcfromtimestamp(d[0]/1000),d[1]] for d in data]
data = fetch_data("MARUTI")
with psycopg2.connect(
host="localhost",
database="tickdata",
port="5432",
user="postgres",
password="passkey",
cursor_factory=RealDictCursor,
) as conn:
with conn.cursor() as cur:
sql = """INSERT INTO pytick (sequence) VALUES (%s) RETURNING *"""
cur.executemany(sql, data)
returning this error cur.executemany(sql, data) TypeError: not all arguments converted during string formatting [Finished in 1.8s]
CodePudding user response:
Your sqlcode is a tuple. It probably shouldn't be.
You're also not using the RETURNING clause, so get rid of it.
All in all, you're maybe looking for something like
data = fetch_data("MARUTI")
with psycopg2.connect(
host="localhost",
database="tickdata",
port="5432",
user="postgres",
password="passkey",
cursor_factory=RealDictCursor,
) as conn:
with conn.cursor() as cur:
sql = "INSERT INTO pytick (id, data) VALUES (%s, %s)"
cur.executemany(sql, data)
to insert all of the 2-tuples in data.
