一、数据库的使用笔记

  关于Sqlite3的使用

  首先是常用命令

sqlite3.connect() 打开一个到 SQLite 数据库文件 database 的链接。
connection.cursor() 创建一个 cursor
cursor.execute() sqlite3模块支持两种类型的占位符:问号和命名占位符(命名样式)
connection.commit() 提交当前的事务。
connection.close()

关闭数据库连接。

  然后是建立一个简单的数据库

    代码如下

    

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Wed May 29 23:45:24 2019
 4 
 5 @author: m1353
 6 """
 7 
 8 import sqlite3
 9 conn = sqlite3.connect('data.db')
10 print("Connect database all green")
11 c = conn.cursor()
12 c.execute('''CREATE TABLE COMPANY
13        (ID INT PRIMARY KEY     NOT NULL,
14        NAME           TEXT    NOT NULL,
15        AGE            INT     NOT NULL,
16        ADDRESS        CHAR(50),
17        SALARY         REAL);''')
18 print("Table created all green")
19 conn.commit()
20 conn.close()

    效果如图

 

    会输出所写的print语句中的字符,就证明了我们创建成功。

    然后会出现一个这样的文件

 

二、爬虫中国最好大学排名

爬的是2017年的

代码如下

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Wed May 29 23:13:27 2019
 4 
 5 @author: m1353
 6 """
 7 
 8 import requests
 9 from bs4 import BeautifulSoup
10 alluniv = []
11 def getHTMLText(url):
12     try:
13         r = requests.get(url,timeout = 30)
14         r.raise_for_status()
15         r.encoding = 'utf-8'
16         return r.text
17     except:
18         return "error"
19 def fillunivlist(soup):
20     data=soup.find_all('tr')
21     for tr in data:
22         ltd =tr.find_all('td')
23         if len(ltd)==0:
24             continue
25         singleuniv=[]
26         for td in ltd:
27             singleuniv.append(td.string)
28         alluniv.append(singleuniv)
29 def writercsv(save_road,num,title):
30     if os.path.isfile(save_road):
31         with open(save_road,'a',newline='')as f:
32             csv_write=csv.writer(f,dialect='excel')
33             for i in range(num):
34                 u=allUniv[i]
35                 csv_write.writerow(u)
36     else:
37          with open('rank.csv','w',newline='')as f:
38             csv_write=csv.writer(f,dialect='excel')
39             csv_write.writerow(title)
40             for i in range(num):
41                 u=allUniv[i]
42                 csv_write.writerow(u)
43 title=["排名","学校名称","省市","总分","生源质量","培养结果","科研规模","科研质量","顶尖成果","顶尖人才","科技服务","产学研究合作","成果转化"]
44 save_road="D:\\test\\rank.csv"
45 def main(num):
46     url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2017.html"
47     html=getHTMLText(url)
48     soup=BeautifulSoup(html,"html.parser")
49     fillunivlist(soup)
50     writercsv(save_road,10,title)
51 main(10)

 

结果如图

先是在输入的路径下储存了一个名字rank.csv文件

然后打开它

 

posted on 2019-05-29 23:38  Lucky_Enterprise  阅读(155)  评论(0编辑  收藏  举报