美文网首页Python爬虫作业
第二周作业(1)拉勾网

第二周作业(1)拉勾网

作者: 谁占了我的一年的称号 | 来源:发表于2017-04-25 20:05 被阅读103次

    这周老师布置的作业爬取拉勾网上,关于“python工程师”,”数据分析师“的岗位。地点我选的是上海。

    Paste_Image.png Paste_Image.png

    其实只有450个,但是它瞎扯有500+
    在观察之后依然还是异步加载的问题,找出真实的网址是啥,然后来解析就好。

    Paste_Image.png

    同时还要注意的是,这个json文件是post方式,所以就要找出post的内容

    Paste_Image.png

    可以看出,post的内容主要有三项:第一项不知道是什么意思,pn代表的是pagenumber,kd代表的是keyword。理解了这些就好办了,程老哥说让我先写单页的,写熟悉框架之后再写scrapy,所以还是先单页的写吧。开整!!

    import json
    import requests
    
    url= 'https://www.lagou.com/jobs/positionAjax.json?px=default&city=%E4%B8%8A%E6%B5%B7&needAddtionalResult=false'
    header={
            'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language':'zh-CN,zh;q=0.8',
            'Cache-Control':'max-age=0',
            'Connection':'keep-alive',
            'Host':'www.lagou.com',
            'Cookie':'JSESSIONID=19A6D78EB668D7BFDD79D4D9DD31CD03; _gat=1; user_trace_token=20170424161622-a7aaefffc7cc431c948e52e6f4580fb1; PRE_UTM=; PRE_HOST=www.baidu.com; PRE_SITE=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3Dmnw7nxYUT1PnGLrygqOgVk7kPCQ6dpbGIaM9rWIINti%26wd%3D%26eqid%3Dbdf78d9a000052e00000000358fdb44d; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2F; LGUID=20170424161622-4d9964b1-28c6-11e7-b26b-5254005c3644; index_location_city=%E4%B8%8A%E6%B5%B7; TG-TRACK-CODE=index_search; SEARCH_ID=f60cf9cc99c74ea8a9a28f1f65e5fa0d; _ga=GA1.2.1813583834.1493021772; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1493021779; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1493021788; LGSID=20170424161622-4d99621e-28c6-11e7-b26b-5254005c3644; LGRID=20170424161639-575e5e25-28c6-11e7-b26b-5254005c3644',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0',
    }
    
    for i in range(1,15):
            data1={'first':'false','pn':'%s'%i,'kd':'Python工程师'}
            html = requests.post(url,data=data1,headers=header).text
            a = json.loads(html)
            results=a['content']['positionResult']['result']
            for poisition in results:
                    name=poisition['positionName']
                    work_year = poisition['workYear']
                    money = poisition['salary']
                    palace = poisition['district']
                    with open('d:\\Python工程师.csv','a+') as f:
                            f.write('{},{},{},{},{}'.format(name,work_year,money,palace,'\n'))
    
    for i in range(1,31):
            data1={'first':'false','pn':'%s'%i,'kd':'数据分析师'}
            html = requests.post(url,data=data1,headers=header).text  
            a = json.loads(html)
            results=a['content']['positionResult']['result']
            for poisition in results:
                    name=poisition['positionName']
                    work_year = poisition['workYear']
                    money = poisition['salary']
                    palace = poisition['district']
                    with open('d:\\数据分析师.csv','a+') as f:
                            f.write('{},{},{},{},{}'.format(name,work_year,money,palace,'\n'))
    

    主要爬了以下几个字段:
    岗位名城
    要求的工作经验
    薪资
    工作地点。
    用pandas将存储下来的表进行一些简单的分析。
    (1)工资:

    Paste_Image.png
    Paste_Image.png

    结合来看,工资平均在12000-21000之间。
    (2)区位:

    Paste_Image.png

    浦东作为上海科技创新中心核心区,科技的需求也是最多的。最弱的是宝山区,只有一个岗位需求。
    总结:

    (1)在写scrapy时构造真实网址的时候, Paste_Image.png

    city后面的是我选择的上海的16进制表达方式。同时要注意post的模式,是要提交参数的。
    (2)在循环爬取的时候,在json文件里找不到最大页数,不知道是我没找到,还是只能是返回网页去看。
    (3) 在用pandas读取从存储下来的csv文件时,要注意改一下编码模式,pandas默认是utf-8,但是这里保存下来的是gbk。

    Paste_Image.png

    相关文章

      网友评论

        本文标题:第二周作业(1)拉勾网

        本文链接:https://www.haomeiwen.com/subject/hbwvzttx.html