標籤:__init__ ogr opera strong ldb table 一個 ret cal
安裝MongoDB: www.mongodb.com/download-center#community下載.msi安裝包→自訂安裝時把路徑首的盤符C改為D,其他如Server\3.x等等都不要改→手動建立倆目錄(日誌D:\Program Files\MongoDB\log\mongodb.log;資料D:\Program Files\MongoDB\data)→D:\Program Files\MongoDB\Server\3.4\bin下按Shift+右鍵進入命令視窗→mongod --logpath "D:\Program Files\MongoDB\log\mongodb.log" --dbpath "D:\Program Files\MongoDB\data"
--install --serviceName "MongoDB"(卸載前的移除是--remove)→net start mongodb(關閉是stop)→瀏覽器開啟http://localhost:27017(報錯則在--install句的末尾加個 --storageEngine=mmapv1 cmd直接用mongo:把這串加入系統內容path:D:\Program Files\MongoDB\Server\3.4\bin;
*****
**************分割線
**************
*****PyCharm裝了Mongo Plugin外掛程式後,右端Mongo Explorer點開是空白:Settings按鈕→
+→Lable隨意→OK;開啟資料表後,點擊右上方的View as table按鈕,把樹型改為表型
*****
**************分割線
**************
*****mongodb使用範例:(爬取花椒直播) import requests,refrom bs4 import BeautifulSoupfrom pymongo import MongoClient def getLiveId(urlOfAnchors): liveIds=set() response=requests.get(urlOfAnchors).text soup=BeautifulSoup(response,‘lxml‘) #正則元字元^後面的(),不再表示group(n),而表示開頭是一串內容 for link in soup.find_all(‘a‘,href=re.compile(‘^(/l/)‘)): href=link[‘href‘] liveId=href.split(‘/‘)[-1] liveIds.add(liveId) return liveIds def getUserId(liveId): liveUrl=‘http://www.huajiao.com/l/{}‘.format(liveId) response=requests.get(liveUrl).text soup=BeautifulSoup(response,‘lxml‘) title=soup.title.get_text() return re.findall(‘\d+‘,title)[0] def getUserData(userId): print(‘正在擷取id為:{}的主播資訊‘.format(userId)) response=requests.get(‘http://www.huajiao.com/user/{}‘.format(userId)).text soup=BeautifulSoup(response,‘lxml‘) userInfo=soup.find(‘div‘,id=‘userInfo‘) data={} data[‘userId‘]=userId data[‘haedPhoto‘]=userInfo.find(‘div‘,‘avatar‘).img[‘src‘] tmp=userInfo.h3.get_text(‘|‘,strip=True).split(‘|‘) data[‘userName‘]=tmp[0] data[‘area‘]=tmp[2] tmp=userInfo.find(‘ul‘,‘clearfix‘).get_text(‘|‘,strip=True).split(‘|‘) data[‘fans‘]=tmp[0] data[‘thumbsUp‘]=tmp[2] data[‘gift‘]=tmp[4] data[‘expense‘]=tmp[6] return data def saveUserData(userData): client=MongoClient() dataBase=client[‘花椒‘] #Mongodb中若無此資料庫,則自動建立 table=dataBase[‘主播資訊‘] table.save(userData) print(‘主播{}資訊儲存成功‘.format(userData[‘userName‘])) if __name__ == ‘__main__‘: urlOfAnchors=‘http://www.huajiao.com/category/1000‘ liveIds=getLiveId(urlOfAnchors) for liveId in liveIds: userId=getUserId(liveId) try: #getUserId()有些liveUrl開啟較慢,跳過它們 userData=getUserData(userId) saveUserData(userData) except: pass 開啟cmd,依次輸入+斷行符號:mongo;show databases;use 花椒;show tables;db.主播資訊.count();db.主播資訊.findOne();db.主播資訊.find()
****************************************分割線****************************************mysql5.7.19:轉自:www.jb51.net/article/119369.htm?utm_source=debugrun&utm_medium=referral 1、安裝&配置:①下載包解壓到E:\mysql-5.7.19-winx64→環境變數の系統變數の建立:變數名MYSQL_HOME,變數值E:\mysql-5.7.19-winx64→path尾添加
;%MYSQL_HOME%\bin→E:\mysql-5.7.19-winx64\bin下建個my.ini檔案(老版本不必;內容在文末);②管理員身份開啟cmd並切到bin目錄下(下文各命令都是在此目錄執行):
mysqld.exe -install(提示:Service successfully installed)→
mysqld --initialize-insecure --user=mysql(初始化建立空密碼的root);③
net start mysql→
mysqladmin -u root -p password 新密碼→
Enter password:(若舊密碼為空白則直接斷行符號)→
net stop mysql(卸載mysql是
mysqld --remove) 2、使用:法①のcmd(不推薦):net start mysql→mysql -u root -p→Enter password:新密碼(…操作樣本:mysql>
show databases;或mysql>
select 語句…;)→mysql>
quit;→net stop mysql 法②のnavicat:net start mysql→開啟軟體navicat→串連→除密碼外隨意→串連測試。 若插入的資料含QQ表情,則建立資料庫時(或右鍵目標庫選資料庫屬性),字元集選末尾的utf8mb4。 查看資料庫資訊?表?欄位:右鍵串連名或庫名選命令列介面→①show create database 庫名;②show create table 表名;③desc xs; 添加使用者:使用者表徵圖→建立使用者→主機名稱為localhost,密碼可空→儲存按鈕;許可權管理員→選擇左側的串連名或下一級的某庫名→添加許可權→左側勾選新使用者→右鍵授予選全部授予→確定。 3、my.ini裡的內容:[client]port=3306default-character-set=utf8[mysqld]port=3306character_set_server=utf8basedir=%MYSQL_HOME%datadir=%MYSQL_HOME%\data[WinMySQLAdmin]%MYSQL_HOME%\bin\mysqld.exe
*****
**************分割線
**************
*****mysql使用範例: import MySQLdb class xiaoshuo: def __init__(self): #串連個事先手動建的utf8庫,否則作用於欄位的charset無效,如表那樣被庫的預設編碼覆蓋了 #MySQLdb與django的DATABASES的不同:port值非str;庫名不叫name;加charset self.conn=MySQLdb.connect( host=‘localhost‘,port=3306,user=‘chengy‘,password=‘‘, db=‘my_db‘,charset=‘utf8‘) #password或passwd,database或db均可 self.cur=self.conn.cursor() # 若用代碼建立資料庫,則要在代碼中分別指定資料庫&表,以及欄位的編碼 # self.cur.execute(‘CREATE DATABASE IF NOT EXISTS my_db default charset utf8‘) # self.conn.set_character_set(‘utf8‘) #欄位的編碼:MySQLdb行,pymysql無此類屬性 # self.conn.select_db(‘my_db‘) self.cur.execute(‘create table if not exists xs(author varchar(10),title text)‘) def operateBase(self): self.cur.execute(‘insert into xs(author) values("小明")‘) self.conn.commit() #self.conn.rollback() # SQL語句除set賦值外,where等值判斷也是用一個=,而非Python中的== self.cur.execute(‘select * from xs where author="小明"‘) print(self.cur.fetchall()) #fetchmany(5),fetchone() print(self.cur.rowcount) def closeBase(self): self.cur.close() self.conn.close() xs=xiaoshuo()xs.operateBase()xs.closeBase()
MongoDB、mysql