import requests, json, time, tablib

def send_ajax_request(data: dict):
try:
ajax_response = session.post(url=ajax_url,
params={"needAddtionalResult": "false", "city": city},
data=data,
headers=ajax_headers,
timeout=timeout)
if ajax_response.status_code == 200:
return ajax_response.json()
return {}
except Exception:
return {} def get_job_info(info_dic: dict):
jobInfoMap = info_dic.get("content").get("positionResult").get("result") for jobInfoDict in jobInfoMap:
dic = {}
dic["companyId"] = jobInfoDict.get("companyId")
dic["companyFullName"] = jobInfoDict.get("companyFullName")
dic["positionName"] = jobInfoDict.get("positionName")
dic["workYear"] = jobInfoDict.get("workYear")
dic["education"] = jobInfoDict.get("education")
dic["salary"] = jobInfoDict.get("salary")
dic["jobNature"] = jobInfoDict.get("jobNature")
dic["companySize"] = jobInfoDict.get("companySize")
dic["city"] = jobInfoDict.get("city")
dic["district"] = jobInfoDict.get("district")
dic["createTime"] = jobInfoDict.get("createTime")
if is_save_txtfile:
yield json.dumps(dic, ensure_ascii=False)
else:
yield dic.values() def save_to_file(json_data):
for data in json_data:
f.write(data + "\n") def save_to_excel(list_data):
for line in list_data:
dataset.append(line) def run():
for i in range(1, 31):
data = {
"first": "false",
"pn": i,
"kd": "python"
}
info_dic = send_ajax_request(data)
data = get_job_info(info_dic)
if is_save_txtfile:
save_to_file(data)
else:
save_to_excel(data)
print("正在保存数据")
time.sleep(sleeptime) if __name__ == '__main__':
session = requests.Session()
job_name = "python"
city = "成都"
timeout = 5
sleeptime = 10
doc_url = "https://www.lagou.com/jobs/list_{job_name}".format(job_name=job_name)
session.headers[
"User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
session.headers["Host"] = "www.lagou.com" doc_response = session.get(url=doc_url, params={"city": city}) ajax_headers = {
"Origin": "https://www.lagou.com",
"Referer": doc_response.url
} ajax_url = "https://www.lagou.com/jobs/positionAjax.json?=false" is_save_txtfile = False if not is_save_txtfile:
dataset = tablib.Dataset()
dataset.headers = ["companyId", "companyFullName", "positionName", "workYear",
"education", "salary", "jobNature", "companySize", "city",
"district", "createTime"] f = open("jobinfo.txt", "a", encoding="utf-8")
try:
run()
except Exception:
print('出错了')
finally:
if is_save_txtfile:
f.close()
else:
with open("jobInfo.xls", "wb") as f:
f.write(dataset.xls)
f.flush()

python3爬虫-通过requests获取拉钩职位信息的更多相关文章

  1. python3爬虫-通过requests获取安居客房屋信息

    import requests from fake_useragent import UserAgent from lxml import etree from http import cookiej ...

  2. 21天打造分布式爬虫-Selenium爬取拉钩职位信息(六)

    6.1.爬取第一页的职位信息 第一页职位信息 from selenium import webdriver from lxml import etree import re import time c ...

  3. python3爬虫-通过selenium登陆拉钩,爬取职位信息

    from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from se ...

  4. python3爬虫抓取智联招聘职位信息代码

    上代码,有问题欢迎留言指出. # -*- coding: utf-8 -*- """ Created on Tue Aug 7 20:41:09 2018 @author ...

  5. ruby 爬虫爬取拉钩网职位信息,产生词云报告

    思路:1.获取拉勾网搜索到职位的页数 2.调用接口获取职位id 3.根据职位id访问页面,匹配出关键字 url访问采用unirest,由于拉钩反爬虫,短时间内频繁访问会被限制访问,所以没有采用多线程, ...

  6. python3爬虫-通过requests爬取图虫网

    import requests from fake_useragent import UserAgent from requests.exceptions import Timeout from ur ...

  7. 通俗易懂的分析如何用Python实现一只小爬虫,爬取拉勾网的职位信息

    源代码:https://github.com/nnngu/LagouSpider 效果预览 思路 1.首先我们打开拉勾网,并搜索"java",显示出来的职位信息就是我们的目标. 2 ...

  8. python3 requests 获取 拉勾工作数据

    #-*- coding:utf-8 -*- __author__ = "carry" import requests,json for x in range(1, 15): url ...

  9. python3爬虫-使用requests爬取起点小说

    import requests from lxml import etree from urllib import parse import os, time def get_page_html(ur ...

随机推荐

  1. vue2.0 学习 ,开始学习

    先看官网的介绍上面的教程   https://cn.vuejs.org/v2/guide/ 尝试 Vue.js 最简单的方法是使用 JSFiddle Hello World 例子.你可以在浏览器新标签 ...

  2. Mac怎么生成.ssh文件

    可使用如下命令生成 1 ssh-keygen -t rsa 因为mac系统也是从unix基础上演变过来的,所以很多核心的东西也是与unix相通的. 1.-t 是指定加密参数为ras,默认是dsa 2. ...

  3. 我的MBTI小测试

    今天做了自己的MBTI测试,选了93道题版本的,测试结果是ESFP表演者型——有我在就有笑声.这个测试很有趣,我也觉得很神奇. 一.我的MBTI图形 二.才储分析:我的性格类型倾向为“ ESFP ”( ...

  4. 利用Vagrant完成开发环境配置

    作者:astaxie链接:https://github.com/astaxie/go-best-practice/blob/master/ebook/zh/01.0.md著作权归作者所有.商业转载请联 ...

  5. C#-XML-数据传输

    http://www.cnblogs.com/fengxuehuanlin/p/5631664.html 关于xml是属于一个比较重要的东西,在平时开发的过程中,这块内容最主要的是要掌握XML内容的读 ...

  6. windows下sqli-labs的搭建及学习(POST篇)

    windows下sqli-labs的搭建及学习(GET篇): http://blog.csdn.net/sherlock17/article/details/64454449 Less-11:基于错误 ...

  7. ZT android -- 蓝牙 bluetooth (二) 打开蓝牙

    android -- 蓝牙 bluetooth (二) 打开蓝牙 分类: Android的原生应用分析 2013-05-23 23:57 4773人阅读 评论(20) 收藏 举报 androidblu ...

  8. 泛型举例:List<T>与DateTable相互转换

    一. DataTable转换到List<T> /// <summary> /// TableToList /// </summary> public class T ...

  9. struts2面试整理

    struts2的工作原理 客户端发送请求 经过一系列的过滤器 FilterDispatcher通过ActionMapper来决定这个REquest需要调用的Action FilterDispather ...

  10. memcached的操作

    memcached是一个高性能的分布式内存对象缓存系统,用于动态web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库次数,从而提高动态.数据库驱动网站的速度.memcached基于 ...