问题是,将爬取的数据保存到数据库里,但是运行后数据只在运行窗口出现,而没有存储到数据库里
from urllib.request import urlopen,Request
from bs4 import BeautifulSoup
import re
import pymysql
for a in range(10):
head = {"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Mobile Safari/537.36 Edg/100.0.1185.39"}
url = "https://movie.douban.com/top250?start={}&filter=".format(a*25)
request = Request(url, headers=head)
respose = urlopen(request)
html = respose.read().decode()
# 连接数据库
connect = pymysql.Connect(
host='localhost',
port=3306,
user='root',
password='201314t',
db='spider_test',
charset='utf8'
)
findImg = re.compile(r'<img.*src="(.*?)"', re.S)
findTitle = re.compile(r'<span class="title">(.*)</span>')
findScore = re.compile(r'<span class="rating_num" property="v:average">(.*)</span>')
findPl = re.compile(r'<span>(.*)</span>')
soup = BeautifulSoup(html, "html.parser")
for item in soup.find_all('div', class_="item"):
item = str(item)
imgSrc = re.findall(findImg, item)[0]
titles = (re.findall(findTitle, item)[0]).split()[0]
score = re.findall(findScore, item)[0]
pl = re.findall(findPl, item)[0]
data = (imgSrc,titles,score,pl)
print(data)
#获取游标
cursor = connect.cursor()
#执行插入语句
sql = "INSERT INTO douban_movie(img_src,pc_name,score,pl) VALUES (%s,%s,%s,%s)"
# cursor.execute(sql,[data])
cursor.close()
connect.commit()
connect.close()
运行结果及报错内容
数据库
不知道是什么原因?数据库是连接上的,没有问题。在倒数第四行代码,没有注释的情况下直接错误