Reputation: 420
I am parsing Wikepedia1 for Neighbourhood and Wikipedia2 for Neighbourhood's latitude and longitude and storing into IBM Db2. I encountered following error code. Please advise.
Version of imports as following
ibm_db 3.0.2
requests 2.24.0
bs4 4.9.1
pandas 1.0.5
I am able to parse until item 102 as following:
HTML of the location where it fails:
Error Code
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-9-035d46749936> in <module>
14 print(primary_key,";",neighbour['href'],";", neighbour['title'],";",lat, lng)
15 insertQuery = f"insert into TORONTO values({primary_key},'{neighbour['title']}','{neighbour['href']}','{lat}','{lng}')"
---> 16 ibm_db.exec_immediate(conn,insertQuery)
Exception: [IBM][CLI Driver][DB2/LINUXX8664] SQL0103N The numeric literal "27s_Bridge" is not valid. SQLSTATE=42604 SQLCODE=-103
my Db2 table as following
create table TORONTO(id INTEGER PRIMARY KEY NOT NULL, neighbourhood VARCHAR(20), href VARCHAR(20), latitude VARCHAR(20), longitude VARCHAR(20) )
My code for parsing
primary_key = 0
for table in soup.find_all('table', class_ = 'multicol'):
for neighbour in table.find_all('a', href=True, class_=False):
url2 = 'https://en.wikipedia.org/' + neighbour['href']
url_object = requests.get(url2)
soup2 = BeautifulSoup(url_object.text, 'html.parser')
count = 0
primary_key += 1
for coor in soup2.find_all('span', class_ = ['latitude','longitude']):
if count == 2: continue
if count == 0: lat = str(coor.string)
if count == 1: lng = str(coor.string)
count += 1
print(primary_key,url2, neighbour['title'],lat, lng)
insertQuery = f"insert into TORONTO values({primary_key},'{neighbour['title']}','{url2}','{lat}','{lng}')"
ibm_db.exec_immediate(conn,insertQuery)
Upvotes: 0
Views: 556
Reputation: 476
Try changing your query to:
insertQuery = f"insert into TORONTO values({primary_key},'{neighbour['title']}','{url2}','{lat}','{lng}')"
Upvotes: 1