我在进行把金融数据存入数据库的案例实战中照着教案本敲好的代码,代码如下:
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('阿里巴巴')
但是运行的结果是这样的
我毫无思路啊!
有谁能告诉我那个步骤错了吗?