![](/img/trans.png)
[英]How can i get and insert formatted text from mysql database and use it
[英]how to INSERT a HTML formatted text to MySQL?
我正在创建一个数据库并插入数据。 我们的后端工程师说他需要一个专栏来保存HTML格式的整篇文章。 但是,当我插入数据时,它给我一个这样的错误:
我检查了错误的确切位置,我发现:
看起来这部分有一些引用或标点符号问题,同一行出现多次。 我使用str()
函数将格式化的HTML文本(使用type()
以查看数据类型为bs4.element.Tag
)转换为字符串,但问题仍然存在。
我的数据库描述是:
('id', 'mediumint(9)', 'NO', 'PRI', None, 'auto_increment')
('weburl', 'varchar(200)', 'YES', '', None, '')
('picurl', 'varchar(200)', 'YES', '', None, '')
('headline', 'varchar(200)', 'YES', '', None, '')
('abstract', 'varchar(200)', 'YES', '', None, '')
('body', 'longtext', 'YES', '', None, '')
('formed', 'longtext', 'YES', '', None, '')
('term', 'varchar(50)', 'YES', '', None, '')
我用来收集全文的功能是:
def GetBody(url,plain=False):
# Fetch the html file
response = urllib.request.urlopen(url)
html_doc = response.read()
# Parse the html file
soup = BeautifulSoup(html_doc, 'html.parser')
#find the article body
body = soup.find("section", {"name":"articleBody"})
if not plain:
return body
else:
text = ""
for p_tag in body.find_all('p'):
text = ' '.join([text,p_tag.text])
return text
我通过这个函数导入数据:
def InsertDatabase(section):
s = TopStoriesSearch(section)
count1 = 0
formed = []
while count1 < len(s):
# tr = GetBody(s[count1]['url'])
# formed.append(str(tr))
# count1 = count1 + 1
(I use this to convert HTML to string, or use the code below)
formed.append(GetBody(s[count1]['url']))
count1 = count1 + 1
这是我的插入功能:
for each in overall(I save everything in this list named overall):
cur.execute('insert into topstories(formed) values("%s")' % (each["formed"]))
解决问题的任何提示?
execute()
函数的语法如下( link ):
cursor.execute(operation, params=None, multi=False)
因此,您可以提供要在查询中使用的值作为execute()
函数的参数。 在这种情况下,它会自动处理值,消除您的问题:
import mysql.connector
cnx = mysql.connector.connect(...)
cur = cnx.cursor()
...
for each in overall:
# If 'each' is a dictionary containing 'formed' as key,
# i.e. each = {..., 'formed': ..., ...}, you can do as follows
cur.execute('INSERT INTO topstories(formed) VALUES (%s)', (each['formed']))
# You can also use dictionary directly if you use named placeholder in the query
cur.execute('INSERT INTO topstories(formed) VALUES (%(formed)s)', each)
...
cnx.commit()
cnx.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.