Python+sqlite爬蟲之實踐


主題:爬取某網站的招聘信息,然后存進Sqlite數據庫。

 

環境准備:

Python3.5

Sqlite

Navicat for SQLite(方便查看)

 

步驟:

一、 安裝Sqlite

下載地址:http://www.sqlite.org/download.html

這里是window 10系統,所以找到Precompiled Binaries for Windows下的sqlite tools下載,解壓后將**sqlite3.exe**放到python的安裝目錄下既可

 

二、初始化Sqlite數據庫

1、在d:\study\spyder目錄下新建job_model.py文件

 1 from peewee import *
 2 
 3 db = SqliteDatabase('job.db')
 4 
 5 class Job(Model):
 6     job_id = IntegerField(unique=True)    #key
 7     salary_min = IntegerField()
 8     salary_max = IntegerField()
 9     job_exp = CharField(max_length=100)
10     company = CharField(max_length=100)
11     company_id = IntegerField()
12     company_info = CharField(max_length=100)
13     url = CharField(max_length=100)
14     attract = CharField(max_length=100)
15     detail = TextField()
16     address = CharField(max_length=100)
17     publish_time = DateField()
18     keyword = CharField(max_length=100)
19     city = CharField(max_length=100)
20     position = CharField(max_length=100)
21     create_time = DateTimeField()
22 
23     class Meta:
24         database = db

 

2、在d:\study\spyder目錄下的命令行中依次輸入,也就是job_model.py所在的目錄

```
python -i job_model.py
db.connect()
db.create_tables([Job])
```

如果此目錄下沒有job.db文件,則新建一個,並且新建一張名job的表,表結構如job_model.py所設計那樣。

如果此目錄已有job.db文件,則在原有的數據上新建一張名job的表。

此時,可以用Navicat連接job.db數據庫查看,是否新增了表job

 

 

三、如何用python將數據寫入Sqlite?

這里將用到python的第三方庫peewee,在命令行輸入pip3 install peewee進行安裝

from spyder.job_model import Job
import peewee

class spyder: 

    ......
  # 這里傳入參數是一個字典
def storeDataToSqlite(self, dic): try: Job.create(job_id = dic['job_id'], salary_min = dic['salary_min'], salary_max = dic['salary_max'], company = dic['company'], company_id = 0,  #先設為0 company_info = dic['company_info'], url = dic['url'], attract = dic['attract'], detail = dic['detail'], address = dic['address'], publish_time = dic['publish_time'], keyword = dic['keyword'], city = dic['city'], job_exp = dic['exp'], position = dic['position'], create_time = self.today_date) except peewee.IntegrityError: print("數據插入錯誤:ID:%s,公司:%s已經存在" % (dic['job_id'],dic['company']))

 

四、新建一個spyder_job.py文件,開始設計編碼

方案一:用requests庫+BeautifulSoup

方案二:用selenium 3+Chrome

思路:搜索城市、招聘關鍵字 --》 頁碼--》爬一個一個招聘的URL --》重復一個一個招聘頁 --》重復爬取信息 --》重復寫入數據庫

 

五、用Navicat連接job.db數據庫查看表job,是否新增了數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM