将爬取数据写入MySQL数据库
import urllib.request import requests import pymysql#连接数据库的包 from lxml import etree db=pymysql.connect('localhost','dsuser','badpassword','dsdb')#地址,用户,密码,数据库名 print('success') cur=db.cursor()#建立游标 cur.execute('DROP TABLE IF EXISTS pk') sql='CREATE TABLE pk(title varchar(50))'#建立数据表 cur.execute(sql) print('创建成功') url='https://www.51job.com/' response=requests.get(url) response.encoding='gbk' html=etree.HTML(response.text) title=html.xpath('//div[@class="cn hlist"]//a/span/text()') for i in range(len(title)): sqle='INSERT INTO pk(title) VALUES(%s)'#插入语句 value=(title[i]) cur.execute(sqle,value) db.commit()#提交数据库 cur.close()