用Python将数据存入数据库失败

问题遇到的现象和发生背景

我在进行把金融数据存入数据库的案例实战中照着教案本敲好的代码,代码如下:

用代码块功能插入代码,请勿粘贴截图
import requests
import re
import pymysql

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
def baidu(company):
    url = 'https://www.baidu.com/s?tn=news&rtt=1&bsst=1&cl=2&wd='+company
    res = requests.get(url, headers=headers).text
    p_href = '

href = re.findall(p_href, res, re.S) # print(href) p_title = '

.*?aria-label="标题:(.*?)"' title = re.findall(p_title, res, re.S) # print(title) p_date = '(.*?)' date = re.findall(p_date, res) # print(date) p_source = '(.*?)' source = re.findall(p_source, res) # print(source) for i in range(len(title)): db = pymysql.connect(host='localhost', port=3308, user='root', password='', database='pachong', charset='utf8') # 插入数据 cur = db.cursor() # 获取会话指针 sql = 'INSERT INTO test (company, title, href, date, source) VALUES(%s, %s, %s, %s, %s)' cur.execute(sql, (company, title[i], href[i], date[i], source[i])) # 执行SQL语句 db.commit() # 固定写法,作用为提交修改 cur.close() # 关闭会话 db.close() # 关闭数据库连接 baidu('阿里巴巴')

运行结果及报错内容

但是运行的结果是这样的

img


代码看起来是一样的,但是和课本上显示的这个结果完全不同,应该是显示这样才对

img

我的解答思路和尝试过的方法

我毫无思路啊!

我想要达到的结果

有谁能告诉我那个步骤错了吗?