准备工作

  1. scrapy startproject Jobs
  2. cd Jobs
  3. scrapy genspider ZhaopinSpider www.zhaopin.com
  4. scrapy crawl ZhaopinSpider
  5. pip install diskcache
  6. pip install tinydb
  7. scrapy crawl ZhaopinSpider -o chongqing.json

ZhaopinSpider

 

# -*- coding: utf-8 -*-
import os
import json from tinydb import TinyDB, Query
from furl import furl
import scrapy class ZhaopinspiderSpider(scrapy.Spider):
name = 'ZhaopinSpider'
allowed_domains = ['www.zhaopin.com', 'sou.zhaopin.com', 'fe-api.zhaopin.com']
start_urls = ['https://www.zhaopin.com/citymap']
cache_db = TinyDB('ZhaopinSpider-cache.json') # 缓存数据库
allowed_cities = ['重庆', ]# '成都', '上海', '深圳', '昆明', '杭州', '贵阳', '宁波'] ## 允许的城市
F = furl('https://fe-api.zhaopin.com/c/i/sou?pageSize=90&kt=3') # URL母版
PAGE_SIZE = 90 # 分页大小 def get_city_code(self, city_name):
'''(根据城市名)获取城市代码'''
Q = Query()
city = self.cache_db.get(Q.name.search(city_name))
if isinstance(city, dict):
return city['code']
else:
print('@' * 100)
print(type(city)) def init_city_info(self, response):
'''初始化城市信息'''
# 取源码
script_text = response.xpath('//script[text()[contains(., "__INITIAL_STATE__")]]/text()').extract_first()
# 去收尾空格
script_text = script_text.strip()
# 预处理为符合json规范的数据
script_json = script_text[script_text.index('=') + 1:]
# 将json字符串转为字典
script_dict = json.loads(script_json)
'''
# 存储取得的json, 便于调试查看
with open('text.json', 'wt', encoding='utf-8') as f:
json.dump(script_dict, f, indent=4, ensure_ascii=False)
'''
'''
city_list = [] # 存储城市列表
# 将字典中的城市提取到列表中,便于查找
for ch in script_dict['cityList']['cityMapList']:
city_list.extend(script_dict['cityList']['cityMapList'][ch])
# 筛选出重庆,并获取城市码
city_code = (list(filter(lambda city: city['name'] == '重庆', city_list)) or [{'code': None}])[0]['code']
'''
for ch in script_dict['cityList']['cityMapList']:
for city in script_dict['cityList']['cityMapList'][ch]:
self.cache_db.insert(city) def parse(self, response):
# if not os.path.exists('ZhaopinSpider-cache.json'):
if not bool(self.eache_db.all()):
self.init_city_info(response)
# 迭代每一个要爬取的城市
for city_name in self.allowed_cities:
# 启动 爬取某个城市 第一个请求
# import ipdb; ipdb.set_trace()
yield self.request_city(city_name) def request_city(self, city_name, page_start=0):
'''构造 爬取某个具体的城市 的请求对象'''
city_code = self.get_city_code(city_name)
url_data = {
'cityId': city_code,
'kw': 'python',
'start': page_start
}
# 要爬取的页面的URL
url = self.F.copy().add(url_data).url
# import ipdb; ipdb.set_trace()
req = scrapy.Request(url, callback=self.parse_city, dont_filter=False)
# 使用 meta 传递附加数据,在 callback 中可以通过 respo.meta 取得
req.meta['city_name'] = city_name
req.meta['page_start'] = page_start
return req def parse_city(self, response):
'''解析具体的页面'''
# 解析json格式的响应结果
resp_dict = json.loads(response.body_as_unicode())
# 总共所能爬取的条数
num_found = resp_dict['data']['numFound']
# 获取当前请求的 page_start
page_start = response.meta['page_start']
# 下一次请求,需要的 start 参数
next_start = page_start + self.PAGE_SIZE
# import ipdb; ipdb.set_trace()
# 判断是否有下一页
if next_start < num_found:
# 获取当前请求的 城市名
city_name = response.meta['city_name']
# 发送下一页请求
yield self.request_city(city_name, page_start=next_start)
# 解析数据
for item in resp_dict['data']['results']:
# TODO: 解析数据,只取我们需要的信息
item['spiderName'] = self.name
# 返回每一条数据
yield item

scrapy 爬取智联招聘的更多相关文章

  1. 用Python爬取智联招聘信息做职业规划

    上学期在实验室发表时写了一个爬取智联招牌信息的爬虫. 操作流程大致分为:信息爬取——数据结构化——存入数据库——所需技能等分词统计——数据可视化 1.数据爬取 job = "通信工程师&qu ...

  2. scrapy项目2:爬取智联招聘的金融类高端岗位(spider类)

    ---恢复内容开始--- 今天我们来爬取一下智联招聘上金融行业薪酬在50-100万的职位. 第一步:解析解析网页 当我们依次点击下边的索引页面是,发现url的规律如下: 第1页:http://www. ...

  3. node.js 89行爬虫爬取智联招聘信息

    写在前面的话, .......写个P,直接上效果图.附上源码地址  github/lonhon ok,正文开始,先列出用到的和require的东西: node.js,这个是必须的 request,然发 ...

  4. Python+selenium爬取智联招聘的职位信息

    整个爬虫是基于selenium和Python来运行的,运行需要的包 mysql,matplotlib,selenium 需要安装selenium火狐浏览器驱动,百度的搜寻. 整个爬虫是模块化组织的,不 ...

  5. 用生产者消费模型爬取智联招聘python岗位信息

    爬取python岗位智联招聘 这里爬取北京地区岗位招聘python岗位,并存入EXECEL文件内,代码如下: import json import xlwt import requests from ...

  6. python爬取智联招聘职位信息(多进程)

    测试了下,采用单进程爬取5000条数据大概需要22分钟,速度太慢了点.我们把脚本改进下,采用多进程. 首先获取所有要爬取的URL,在这里不建议使用集合,字典或列表的数据类型来保存这些URL,因为数据量 ...

  7. python爬取智联招聘职位信息(单进程)

    我们先通过百度搜索智联招聘,进入智联招聘官网,一看,傻眼了,需要登录才能查看招聘信息 没办法,用账号登录进去,登录后的网页如下: 输入职位名称点击搜索,显示如下网页: 把这个URL:https://s ...

  8. scrapy框架爬取智联招聘网站上深圳地区python岗位信息。

    爬取字段,公司名称,职位名称,公司详情的链接,薪资待遇,要求的工作经验年限 1,items中定义爬取字段 import scrapy class ZhilianzhaopinItem(scrapy.I ...

  9. python3 requests_html 爬取智联招聘数据(简易版)

    PS重点:我回来了-----我回来了-----我回来了 1. 基础需要: python3 基础 html5 CS3 基础 2.库的选择: 原始库  urllib2  (这个库早些年的用过,后来淡忘了) ...

随机推荐

  1. php中上传图片,原生代码

    一.实现图片上传方法一 html的写法: <form action="handle.php" name="form" method="post& ...

  2. C# Global定时执行Global文件aTimer处理

    public class Global : System.Web.HttpApplication { private static event Action eventActions; /// < ...

  3. C# 反射获取属性值、名称、类型以及集合的属性值、类型名称

    实体类 class Product { public string Id { get; set; } public string Name { get; set; } public List<P ...

  4. 2018-2019-2 20165205 《网络对抗技术》 Exp1 PC平台逆向破解

    2018-2019-2 20165205 <网络对抗技术> Exp1 PC平台逆向破解 1. 实验任务 1.1实验概括 用一个pwn1文件. 该程序正常执行流程是:main调用foo函数, ...

  5. caffe学习笔记1

    博客 http://blog.csdn.net/seven_first/article/details/47378697 https://zhuanlan.zhihu.com/p/25127756?r ...

  6. 显示开机信息-dmesg

    显示开机信息-dmesg kernel会将开机信息存储在ring buffer中.您若是开机时来不及查看信息,可利用dmesg来查看.开机信息亦保存在/var/log目录中,名称为dmesg的文件里. ...

  7. Anatomy of a Database System学习笔记 - 事务:并发控制与恢复

    这一章看起来是讲存储引擎的.作者抱怨数据库被黑为“monolithic”.不可拆分为可复用的组件:但是实际上除了事务存储引擎管理模块,其他模块入解析器.重写引擎.优化器.执行器.访问方式都是代码相对独 ...

  8. SpringMVC参数绑定总结

    springMvc作用:    a) 接收请求中的参数    b) 将处理好的数据返回给页面参数绑定(就是从请求中接收参数):    a) 默认支持的类型: request, response, se ...

  9. FIN_WAIT_2状态解释

    关于网络设备的FIN_WAIT_2状态解释出处:http://hi.baidu.com/netdemon1981/blog/item/584bfbb2aeb1d4acd9335ad9.html 在HT ...

  10. 没有upcast 也不会发生多态

    class A{ public: virtual void f(){ cout << "A::f()"<<endl;} }; class B:public ...