pymysql
#导入pymsql模块
#链接数据库,注意port是int型,不是str,所以不要用引号conn
=
pymysql.connect(
user='root',
password='root',
host='127.0.0.1',
port=3306,
database='test_demo',
use_unicode=True,
charset="utf8"
)#获取游标cursor
=
conn.cursor()#插入数据,注意看有变量的时候格式cursor.execute("INSERT
INTO
tieba_user(`uuid`,`user_name`,`user_rank`,`user_level`,`level_status`,`tieba_name`)
VALUES
(%s,%s,%s,%s,%s,%s)",
(user_id,
user_name,
user_rank,
user_level,
level_statu,
tieba_name))#提交conn.commit()#关闭连接cursor.close()
使用mongodb很简单。首先安装pymongo:
1
pip install pymongo
代码实现:用urllib2读取数据,打包成JSON格式插入到mongodb中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from pymongo import MongoClient
try:
from urllib2 importurlopen, Request, HTTPError, URLError
except ImportError:
from urllib.request import urlopen, Request, HTTPError, URLError
result = []
try:
f = urlopen('http://www.dynamsoft.com', timeout=3)
while 1:
tmp = f.read(10240)
if len(tmp) == 0:
break
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)