码迷,mamicode.com
首页 > 编程语言 > 详细

Python小爬虫练习

时间:2015-12-06 22:27:09      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

# coding: utf-8
__author__ = ‘zhangcx‘
from urllib3 import PoolManager
import codecs
import json

class myjob(object):
    def __init__(self):
        self._page = 1
        self._totalPageCount = 0
        self._first = True
        self._hasNextPage = True
        self._http =  PoolManager()

    def getjob(self):
        if(self._hasNextPage):
                r = self._http.request(‘POST‘,‘http://www.lagou.com/jobs/positionAjax.json?px=default&city=%E6%AD%A6%E6%B1%89‘
                                       ,{‘first‘:‘%s‘ % self._first,‘pn‘:‘%d‘ % self._page,‘kd‘:‘Java‘})
                items = json.loads( r.data.decode(‘utf-8‘))
                for item in items[‘content‘][‘result‘]:
                    print("{name},{positionName},{salary}".format(name=item[‘companyShortName‘],positionName = item[‘positionName‘],salary=item[‘salary‘]))
                #print(items)
                self._hasNextPage = items[‘content‘][‘hasNextPage‘]
                self._totalPageCount = items[‘content‘][‘totalPageCount‘]

                if(self._page > 0):
                    self._first = ‘false‘
                if((self._page+ 1) > self._totalPageCount):
                    self._hasNextPage = False
                self._page += 1
                self.getjob()


if __name__ == "__main__":
    job = myjob()
    job.getjob()


技术分享

 

Python小爬虫练习

标签:

原文地址:http://www.cnblogs.com/huangzelin/p/5024452.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!