python3 爬虫连接mysql
来源:互联网 发布:淘宝客关闭返利 编辑:程序博客网 时间:2024/05/21 06:24
进入mysql创建一个数据库
create database places;use places;
创建一张表
create table place(id int primary key auto_increment,name varchar(20),longitude varchar(20),latitude varchar(20)) auto_increment = 1;
编写爬虫文件file.py
import jsonimport urllib.requestimport reimport sysimport pymysqlurl = 'http://api.sou-yun.com/api/Biography?author=&scope=&beginYear=0&endYear=0'html = urllib.request.urlopen(url)s = json.loads(html.read().decode('utf-8'))db = pymysql.connect(host = 'localhost',port = 3306,user = 'root',password = '******',db = 'places',charset = 'utf8')cur = db.cursor()print ("地里位置:")place = s["Traces"][0]["Markers"]for x in place:try:insert_sql = "insert into place (name, longitude, latitude) values ('%s', '%s', '%s')"data = (x["Title"], x["Latitude"], x["Longitude"])cur.execute(insert_sql % data)db.commit()print (x["Title"])print ("纬度: %s" %(x["Latitude"]))print ("经度: %s" %(x["Longitude"]))print ()except:print ("error")db.rollback()
编译运行这个py文件
python3 file.py
去数据库中查看
mysql> select * from place;+-----+--------------------------------+------------+------------+| id | name | longitude | latitude |+-----+--------------------------------+------------+------------+| 1 | 濮阳 | 35.7675858 | 115.035919 || 2 | 邯郸 | 36.63122 | 114.545807 || 3 | 运城 | 35.0325851 | 111.013382 || 4 | 平顶山 | 33.7720528 | 113.198936 || 5 | 绍兴 | 30.0365181 | 120.58667 || 6 | 鹤壁 | 35.752655 | 114.304047 || 7 | 西安 | 34.347435 | 108.946304 || 8 | 渭南 | 34.5056877 | 109.516739 || 9 | 安阳 | 36.10365 | 114.3996 || 10 | 潍坊 | 36.71321 | 119.168137 || 11 | 三门峡 | 34.7784424 | 111.206833 || 12 | 扬州 | 32.39986 | 119.419106 || 13 | 宜春 | 27.82009 | 114.422684 || 14 | 九江 | 29.7113285 | 116.007996 || 15 | 保定 | 38.8800545 | 115.471054 || 16 | 六安 | 31.7412262 | 116.529648 || 17 | 襄阳 | 32.01479 | 112.129089 || 18 | 武威 | 37.93408 | 102.644524 || 19 | 张掖 | 38.9321861 | 100.456223 || 20 | 蒙古 | 47.966156 | 107.149559 |......
0 0
- python3 爬虫连接mysql
- 爬虫学习笔记二、 python3.4连接mysql数据库
- python3:爬虫并存入mysql
- Python3连接MySQL数据库
- python3.4连接mysql
- python3.4连接mysql
- python3.5连接 mysql
- Python3连接MySQL数据库
- Python3.6连接MySQL
- python3.5 连接 mysql
- Python3.6连接MySQL
- Python3.4连接MySQL
- python3连接MySQL数据库
- Python3连接MySQL
- Python3 连接mysql
- python3.6连接MySQL
- python3连接mysql
- python3连接MySQL数据库
- iOS内购步骤详情,从申请税收协议开始
- 校验手机号和获取验证码
- 防止程序猿和前端狗打架的几条约定
- svn报错:“Previous operation has not finished; run 'cleanup' if it was interrupted“ 的解决方法
- 动态规划练习03:采药
- python3 爬虫连接mysql
- 跳跃表
- Java程序包含继承时的子父类执行顺序
- ContentProvider和Cursor以及CursorAdapter三者之间内部链接实现原理 解析
- 小莫的成神之旅(二)纯css3实现翻转效果
- Android 6.0 SystemUI之通知栏下拉时周边全透明
- Android开发 adb命令简介
- hello csdn
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)