首先需要在官网下载MySQL,https://dev.mysql.com/downloads/installer/
在官网下载python3.5,https://www.python.org/downloads/release/python-354/
安装python3.5,安装MySQL,因为python3.5并没有官网的mysql接口,所以采用第三方库pymysql,pip install pymysql
采用以下例验证是否安装成功:
http://database.51cto.com/art/200511/12524.htm
设置主键,避免重复输入数据:https://www.cnblogs.com/haodawang/p/5967222.html,https://www.cnblogs.com/prayer21/p/6018864.html
python例程:http://www.runoob.com/mysql/mysql-drop-tables.html
之后可以编写python爬虫了,通过urllib,得到网页代码,http://blog.csdn.net/dangyang1992/article/details/53435016
教程有https://cuiqingcai.com/1052.html,本文目前简单的方法,即from urllib.request import urlopen和import re,urllib.request用于获取数据,re正则表达式提取数据。
比较好的爬虫是requests库https://cuiqingcai.com/2556.html,和Beautiful Soup库https://cuiqingcai.com/1319.html,
更高级是采用爬虫框架Scrapy。
# -*- coding: UTF-8 -*-
import urllib.request
page = urllib.request.urlopen(url)
html = page.read()
print (html)
import pymysql
# 打开数据库连接
db = pymysql.connect("localhost","test","123456","TESTDB" )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
#create database TESTDB;
#use TESTDB;
#create table EMPLOYEE( FIRST_NAME char(20), LAST_NAME char(20), AGE int(11), SEX char(1), INCOME float, primary key(FIRST_NAME) );
#DESCRIBE EMPLOYEE;
## SQL 删除语句
#sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (10)
#try:
# # 执行SQL语句
# cursor.execute(sql)
# # 提交修改
# db.commit()
#except:
# # 发生错误时回滚
# db.rollback()
# SQL 插入语句
sql = "INSERT IGNORE INTO EMPLOYEE(FIRST_NAME, \
LAST_NAME, AGE, SEX, INCOME) \
VALUES ('%s', '%s', '%d', '%c', '%d' )" % \
('Mac1', 'Mohan', 20, 'M', 2000)
try:
# 执行sql语句
cursor.execute(sql)
# 执行sql语句
db.commit()
except:
# 发生错误时回滚
db.rollback()
print("except")
# SQL 查询语句
sql = "SELECT * FROM EMPLOYEE \
WHERE INCOME > '%d'" % (1000)
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
fname = row[0]
lname = row[1]
age = row[2]
sex = row[3]
income = row[4]
# 打印结果
print ("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
(fname, lname, age, sex, income ))
except:
print ("Error: unable to fetch data")
# 关闭数据库连接
db.close()