I want to insert my scraped data directly into PostgreSQL db, I'm struggling with writing query for this, any help would be appreciated.
The code I've came up with so far:
import csv
import urllib.request
from bs4 import BeautifulSoup
conn = psycopg2.connect(database='--',user='--', password='--', port=--)
cursor = conn.cursor()
soup = BeautifulSoup(urllib.request.urlopen("http://tis.nhai.gov.in/TollInformation?TollPlazaID=236").read(),'lxml')
tbody = soup('table' ,{"class":"tollinfotbl"})[0].find_all('tr')
for row in tbody:
cols = row.findChildren(recursive=False)
cols = [ele.text.strip() for ele in cols]
writer.writerow(cols)
print(cols)
My table's details are as follows:
Column | Type | Modifiers
---------------+---------+-----------
vehicle_type | text | not null
one_time | integer | not null
return_trip | integer |
monthly_pass | integer | not null
local_vehicle | integer | not null