1. 程式人生 > >Python3 爬取51job的資料存入MongoDB並分析

Python3 爬取51job的資料存入MongoDB並分析

1.開啟51job首頁,輸入Python,地址選擇深圳,得到搜尋頁面:

3.不同點:

items.py新增如下程式碼:

from scrapy import Item,Field

class JobsItem(Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    job = Field()
    company = Field()
    area = Field()
    salary = Field()
    datetime = Field()

settings.py新增如下程式碼:

ROBOTSTXT_OBEY = False
#模擬瀏覽器,應對反爬
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
#解決字元亂碼的問題
FEED_EXPORT_ENCODING = 'gbk'

ITEM_PIPELINES = {
    'jobs.pipelines.MongoPipeline': 300,
}

MONGO_URL = 'localhost'
MONGO_DB = '51job'

spider資料夾類的py檔案新增如下程式碼:

# -*- coding: utf-8 -*-
import scrapy
import time
from jobs.items import JobsItem


class A51jobSpider(scrapy.Spider):
    name = 'a51job'
    allowed_domains = ['search.51job.com']
    # start_urls = ['http://www.51job.com/']
    start_urls = ["https://search.51job.com/list/040000,000000,0000,00,9,99,python,2,1.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="]

    def parse(self, response):
        infos = response.css('.el')
        for info in infos:
            item = JobsItem()
            job = info.css('a::attr("title")')
            if len(job) == 0:
                continue
            item['job'] = info.css('a::attr("title")').extract_first().strip()
            item['company'] = info.css('span a::attr("title")').extract()[-1].strip()
            item['area'] = info.css('.t3::text').extract_first().strip()
            item['datetime'] = info.css('.t5::text').extract_first().strip()
            salary = info.css('.t4::text')
            if len(salary) == 0:
                yield item
                continue
            item['salary'] = info.css('.t4::text').extract_first().strip()
            yield item

        time.sleep(1)
        url = response.css('.bk a::attr("href")').extract()[-1]  #查詢下一頁的連結
        yield scrapy.Request(url = url,callback = self.parse)    #解析下一頁

執行,儲存至MongoDB資料庫,如圖:

分析:

In [1]: import pymongo

In [2]: client = pymongo.MongoClient(host = 'localhost',port = 27017)

In [3]: db = client['51job']

In [4]: collection = db.JobsItem

In [5]: collection.find().count()
C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts\ipython:1:
 DeprecationWarning: count is deprecated. Use Collection.count_documents instead
.
Out[5]: 5326

一共有5326條招聘資訊。

#查詢今日釋出的前50條招聘
In [6]: results = collection.find({'datetime':{'$gt':'10-06'}}).limit(50)

In [7]: for result in results:
    ...:     print ("公司:{}\t薪水:{}".format(result.get('company'),result.get(
    ...: 'salary')))
    ...:
公司:深圳市度點科技有限公司     薪水:0.8-2萬/月
公司:深圳市恆牛科技有限公司     薪水:3-4萬/月
公司:深圳市光速度科技有限公司   薪水:6-9千/月
公司:深圳市德梅寒科技有限公司   薪水:2-2.5萬/月
公司:深圳市卓達電子有限公司     薪水:1-1.6萬/月
公司:深圳市易思博酷客科技有限公司       薪水:1-1.8萬/月
公司:睿思商業智慧(深圳)有限公司       薪水:4.5-7千/月
公司:達觀資料   薪水:1.8-3.6萬/月
公司:深圳德聚企業管理諮詢有限公司       薪水:2-4萬/月
公司:深圳飛豹航天航空科技有限公司       薪水:1-1.5萬/月