I'm trying to store some fields derived from a webpage in mysql table. The script that I've created can parse the data and store them in the table. However, as the username is non-english, the table stores the name as ????????? ?????????
instead of Αθανάσιος Σουλιώτης
.
Script I've tried with:
import mysql.connector
import requests
from bs4 import BeautifulSoup
link = 'https://stackoverflow.com/questions/67941060/web-scraper-update'
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd = "",
database="mydatabase",
charset='utf8',
use_unicode=True
)
mycursor = mydb.cursor()
mycursor.execute("DROP TABLE if exists webdata")
mycursor.execute("CREATE TABLE if not exists webdata (title VARCHAR(255), username VARCHAR(255), reputation VARCHAR(255))")
response = requests.get(link)
soup = BeautifulSoup(response.text,"lxml")
post_title = soup.select_one("h1[itemprop='name'] > a").get_text(strip=True)
username = soup.select_one(".user-details > a").get_text(strip=True)
reputation = soup.select_one("span.reputation-score").get_text(strip=True)
print((post_title,username,reputation))
mycursor.execute(
"INSERT INTO webdata (title,username,reputation) VALUES (%s,%s,%s)",
(post_title,username,reputation)
)
mydb.commit()
mydb.close()
This is how the output are being printed in the console:
('Web scraper update', 'Αθανάσιος Σουλιώτης', '13')
The database stores the output as:
'Web scraper update', '????????? ?????????', '13'
How can I store non-english name in mysql table accordingly?