爬虫数据写入数据库时报错
import requestsimport re
import openpyxl
import sqlite3
file_name = re.compile(r'title="(.*?)"')
link_ = re.compile(r"<a href='(.*?).html'")
url = 'http://www.dytt89.com'
def main(url):
head = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37"
}
response = requests.get(url,headers=head)#verify = False去除安全验证
response.encoding = 'gb2312'
tx = re.findall(file_name,response.text)
del tx
return tx
def get_link(url):
#获取电影名对应链接
head = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37"
}
response = requests.get(url,headers=head)#verify = False去除安全验证
response.encoding = 'gb2312'
tx = re.findall(file_name,response.text)
tx2 = re.findall(link_,response.text)
list1 = []
for each in tx2:
list1.append(url+each)
list1.reverse()
return list1
def save_pave(tx,list1):
connet = sqlite3.connect("电影名.db")
cur = connet.cursor()
sql = '''
create table if not exists 电影名称
(电影名 varchar(20),
电影链接 varchar(30)
);
'''
cur.execute(sql)
connet.commit()
for each1 in tx:
for each_one in list1:
sql ='''
insert into 电影名称(电影名,电影链接) values ('%s','%s')
'''%(each1,each_one)
cur.execute(sql)
continue
connet.commit()
cur.close()
connet.close()
sqlite3.IntegrityError: UNIQUE constraint failed: 电影名称.电影名 单运行数据库的操作函数没有报错,请把你的代码和报错发全。用代码格式贴出来。 数据库名和字段名一样??? https://blog.csdn.net/weixin_41147129/article/details/86706473
页:
[1]