【发布时间】:2021-08-09 13:59:44
【问题描述】:
Code:
import mysql.connector as MYSQL
from bs4 import BeautifulSoup
import requests
URL = 'https://parade.com/937586/parade/life-quotes/'
SQL = 'INSERT INTO quoteslist (id, Quotes) VALUES (%s,%s)'
CONFIG = {
'user': 'root',
'password': 'Demon@',
'host': '127.0.0.1',
'database': 'my_quotes',
'charset': 'utf8mb4'
}
SELECT = 'span[data-parade-type="promoarea"] .figure_block ~ p'
GT = {'strip': True, 'separator': ' '}
with requests.Session() as session:
web_page = session.get(URL)
web_page.raise_for_status()
soup = BeautifulSoup(web_page.text, "html.parser")
quote = [(x.get_text(**GT)) for x in soup.select(SELECT)]
with MYSQL.Connect(**CONFIG) as db:
mycursor = db.cursor()
for q in quote:
idx = q.split()[0]
if idx[0].isdigit():
text = q[len(idx):].strip()
params = (idx.replace('.', ''), text)
mycursor.execute(SQL, params)
db.commit()
输出错误:
line 29, in <module> mycursor.execute(SQL, params)
mysql.connector.errors.DataError: 1406 (22001): Data too long for column 'Quotes' at row
1
我尝试在 URL 中插入所有 150 个引号的数据,在第 1 行中获取错误数据太长。 我对那个错误没有这么深的想法。谁能告诉我如何纠正错误?
【问题讨论】:
-
@ahmed 美国否
-
是否有理由使用不同的用户名发布您的问题?万一你超过了限制。那你就等着
-
@ahmed American 你知道哪个代码出错了,在第 1 行错误中显示数据太长吗?
标签: python mysql beautifulsoup