當前位置:首頁 » 數據倉庫 » python保存數據到資料庫
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

python保存數據到資料庫

發布時間: 2022-08-23 05:16:35

⑴ 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多保存數據和方法,如
存在在變數中,只要程序不退出,數據就存在
存放在文件中,這樣數據會一直存在
存放在資料庫中