想写一个把爬虫数据写入MySQL的代码,数据库设置为id和name,主键是id而且设置成递增。
import pymysql
import requests
from lxml import etree
headers = {
'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
}
url = 'https://www.bilibili.com/ranking/all/1/0/3'
response = requests.get(url,headers=headers)
text = response.text
html=etree.HTML(text)
items=html.xpath('//*[@id="app"]/div[1]/div/div[1]/div[2]/div[3]/ul/li/div[2]/div[2]')
for item in items:
name=item.xpath('./a')[0].text
db = pymysql.connect(host ='localhost',user='root',password ='root'
,port =3306,db = 'spiders')
cursor = db.cursor()
sql = 'insert into rank(id,name) values(null,%s)'
for item in items:
name=item.xpath('./a')[0].text
cursor.execute(sql, (name))
db.commit()
db.close()
你这都不给出报错信息,让大家猜?
id既然是自增,就不用在插入语句中写这一列。
二楼说的对
如果ID用的自增序列的话ID还是需要赋值的;既然ID是主键,就不能设置null