IOSAndroid开发程序员

30分钟写出一个51job职位爬虫,0基础小白也能写

2018-03-23  本文已影响237人  月球在此

30分钟写出一个51job职位小爬虫,小白也能写的出来,不用懂正则, 不用懂xpath,分分钟写出来,大神走开

前提环境:你的电脑里装过python 2/3 和 pip(python包管理工具)

├── README.md
└── my51JobSpider
    ├── my51JobSpider
    │   ├── __init__.py
    │   ├── __pycache__
    │   ├── items.py
    │   ├── middlewares.py
    │   ├── pipelines.py
    │   ├── settings.py
    │   └── spiders
    │       ├── __init__.py
    │       └── __pycache__
    └── scrapy.cfg

下面来简单介绍一下各个主要文件的作用:

scrapy.cfg :项目的配置文件
/ :项目的Python模块,将会从这里引用代码
items.py :项目的目标文件
pipelines.py :项目的管道文件
settings.py :项目的设置文件
spiders/ :存储爬虫代码目录

Snip20180323_15.png

start_urls = () :爬取的URL元祖/列表。爬虫从这里开始抓取数据,所以,第一次下载的数据将会从这些urls开始。其他子URL将会从这些起始URL中继承性生成。

parse(self, response) :解析的方法,每个初始URL完成下载后将被调用,调用的时候传入从每一个URL传回的Response对象来作为唯一参数,主要作用如下:

负责解析返回的网页数据(response.body),提取结构化数据(生成item)
生成需要下一页的URL请求。

    def parse(self, response):
        liststype = response.xpath('/html/body/div[5]/div[2]//div//a/@href')
        for url in liststype:
            print url.extract()

运行看看效果~~~~~ 兜兜转转介绍了很多基础知识 ~~~ 10分钟过去了


Snip20180323_28.png
    def parse(self, response):
        liststype = response.xpath('/html/body/div[5]/div[2]//div//a/@href')
        for url in liststype:
            yield scrapy.Request(url=url.extract(),callback=self.parseSearch)

        pass
    def parseSearch(self,response):
        listsjob = response.xpath('//*[@id="resultList"]//div/p/span/a/@href')
        listpages = response.xpath('//div[@class="p_in"]/ul/li/a/@href')
        for page in listpages:
            yield scrapy.Request(url=page.extract(),callback=self.parseSearch)
        for url in listsjob:
            yield scrapy.Request(url=url.extract(),callback=self.parseDesc)
        pass

    def parseDesc(self,response):
        context= response.text
        title = response.xpath('/html/body/div[3]/div[2]/div[2]/div/div[1]/h1/text()').extract()[0]
        area = response.xpath('//span[@class="lname"]/text()').extract()[0]
        money = response.xpath('/html/body/div[3]/div[2]/div[2]/div/div[1]/strong/text()').extract()[0]
        exp = response.xpath('/html/body/div[3]/div[2]/div[3]/div[1]/div/div/span[1]/text()').extract()[0]
        study = response.xpath('/html/body/div[3]/div[2]/div[3]/div[1]/div/div/span[2]/text()').extract()[0]
        all = response.xpath('/html/body/div[3]/div[2]/div[2]/div/div[1]/p[2]/text()').extract()[0]
        lists = all.replace(' ','').replace('\r','').replace('\t','').split('|')
        company = lists[0]
        people = lists[1]
        type = lists[2]

        print (title)
        item = My51JobspiderItem()
        item['title'] = title
        item['area'] = area
        item['money'] = money
        item['company'] = company
        item['people'] = people
        item['type'] = type
        item['study'] = study
        item['exp'] = exp
        yield item
        pass

看看效果~~ good


image.png

总结:

娱乐贴~~ 给大家多一个无聊时的乐趣,xpath还是有很多语法需要学习,还有scrapy,有问题直接问,保证回答但是不许骂我,还有建议大家不要在繁忙时间随便爬,友好一点,凌晨两三点去偷偷的干
github地址:https://github.com/CZXBigBrother/51JobSpider

上一篇下一篇

猜你喜欢

热点阅读