I wrote a web scraping code to get some data from a website that works so far. I connected Python to SQL because I need the data to be MySQL saved. The connection between Python and SQL is working fine, I created the SQL table with Python which I commented on the code below. My problem is while insertion the data into the SQL table, the insert script should be working fine but I'm getting a syntax error that I couldn't correct. Does anybody have an idea?
Here is the code:
import mysql.connector
db_connection = mysql.connector.connect(
host="141.45.91.40",
user="s0566293",
passwd="*******",
database="s0566293_projekt"
)
db_cursor = db_connection.cursor()
#Here creating database table as film'
#db_cursor.execute("CREATE TABLE Filme (filmName VARCHAR(255))") #already exists.
#Get database table'
db_cursor.execute("SHOW TABLES")
for table in db_cursor:
print(table)
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import requests
import re
my_url = 'https://www.berlin.de/kino/_bin/azfilm.php'
# opening up connection , grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
#html parsing
page_soup = soup(page_html,"html.parser")
# grab any Zeichen
az = page_soup.findAll("div",{"class":"block"})
fil = az[0]
sei = fil.findAll("li")
for s in sei:
i=s.text
if (i == '#'):
continue
else:
seite = "https://www.berlin.de/kino/_bin/azfilm.php/de/{}".format(i)
r = requests.get(seite)
seiten_soup = soup(r.content, 'html.parser')
#print page url
#print("-------------------------------------------------------\n" + seite)
#Filme jeder Seite
# opening up connection , grabbing the page
uClient = uReq(seite)
page_html = uClient.read()
uClient.close()
#html parsing
page_soup = soup(page_html,"html.parser")
# grab any film
az = page_soup.findAll("div",{"class":"inner"})
fil = az[0]
filme = fil.findAll("li")
for film in filme:
db_cursor.execute("INSERT INTO Filme(filmName) VALUES("+film.contents[0].contents[0]+")")
db_cursor.execute("SHOW Filme")
#print(film.contents[0].contents[0])
db_cursor.execute("INSERT INTO Filme(filmName) VALUES(%s)", (film.contents[0].contents[0]))i have tried this, it still doesnt work though.contents[0]. That's needed to make the second argument a tuple.