⑴ python把爬到的数据放到数据库
去装一个 pymsql包
然后
import 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()
类似这样
⑵ 怎么把python 用urllib2爬取的内容 存到数据库中
使用mongodb很简单。
首先安装pymongo:
1pip install pymongo
代码实现:用urllib2读取数据,打包成JSON格式插入到mongodb中。
pymongo import MongoClienttry: from urllib2 importurlopen, Request, HTTPError, URLErrorexcept ImportError: from urllib.request import urlopen, Request, HTTPError, URLErrorresult = []try: f = urlopen('http://www.dynamsoft.com', timeout=3) while 1: tmp = f.read(10240) if len(tmp) == 0: break else: result.append(tmp) f.close()except HTTPError, URLError: print URLError.codecontent = ''.join(result)post = {"author": "yushulx","content": content}client = MongoClient()db = client.test_databaseposts = db.postsposts.insert(post)print posts.find_one({"author": "yushulx"})
⑶ 怎么将python爬取的数据存如数据库中
使用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
⑷ 从数据库里python获取数据存到本地数据库
python项目中从接口获取数据并存入本地数据库
首先用postman测试接口
根据请求方式将数据存入数据库中
首先用postman测试接口
通过url,选择相应的请求方式,头部,数据格式,点击send看能否获取数据
根据请求方式将数据存入数据库中
下面是post请求方式def get() URL = '' HEADERS = {'Content-Type': 'application/json'} JSON = {} response = request.post(URL,headers=HEADERS,json=JSON) #json.loads()用于将str类型的数据转成dict jsondata = json.load(response.txt) for i in jsondata: date1 = i[data] type1 = i[type] ... #拼接sql语句 sql="" conn=MySQLdb.connect(host="localhost",user="root",passwd="sa",db="mytable") cursor=conn.cursor() ursor.execute(sql)
⑸ 如何将python爬虫数据直接存到数据库
直接使用sqlite 这个是python自带的
用起来也很简单网络一下教程就行 ,很容易就把数据写到数据库里了
⑹ Python怎么把数据框内数据写入数据库
f = open("a.txt", "w")for i in range(1, 10): f.write("<user>\n <id>"+str(i)+"</id>\n</user>\n")f.close()
因为i是int型,所以要先转为str型,再进行字符串拼接,然后写入文件
⑺ python多进程数据库储存问题
粗看一下,估计pool.map里开启了多进程。
问题是,每个进程访问数据库,要有各自的cursor,要各自去commit才可以。
⑻ Python 之excel文本数据存储到mysql数据库中
Python 之excel文本数据存储到mysql数据库中
with open("fileinfo.txt", "rt") as handle:
datas = [ln.split('\t') for ln in handle]
conn = MySQLdb.connect(host='localhost', user='mysql', passwd='', db='db')
curr = conn.cursor()
curr.executemany("insert into tb values (?, ?, ?)", datas)
conn.commit()
⑼ python如何保存数据
python有N多保存数据和方法,如
存在在变量中,只要程序不退出,数据就存在
存放在文件中,这样数据会一直存在
存放在数据库中