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. openlayers研究(一) 初始化流程

    下载2.13.1.解压缩.根据readme解释,openlayers.js是一个压缩库,.light是一个图像显示的简化库,mobile顾名思义应该是应对移动设备的库.build里面有py写的打包工具 ...

  2. 使用WICleanup清理Windows Installer 冗余文件

    使用WICleanup清理Windows Installer 冗余文件 | 浏览:816 | 更新:2015-11-02 10:43 | 标签:Win7 Win10 1 2 3 4 5 6 7 分步阅 ...

  3. MUI框架-07-HBuilder+夜神安卓模拟器

    MUI框架-07-HBuilder+夜神安卓模拟器 有时候我们在 HBuilder 里面 web 浏览器预览我们的 MUI 项目界面时,总感觉这个 web 浏览器随便拖拉比例,大小可调,但它毕竟是浏览 ...

  4. RequestMapping 支持的方法

    基于版本:Spring Framework 4.3.5.RELEASE 1.支持指定多个方法 @RequestMapping(value = "/index", method = ...

  5. django从1.7升级到1.9后 提示:RemovedInDjango110Warning

    Django项目,把django从1.7升级到1.9后,大量报错.需要做如下修改. 1,修改urls.py: 在django1.9里,urls的配置不再支持字符串型的路由.需要先import,然后直接 ...

  6. 使用Virtual Audio Cable软件实现电脑混音支持电脑录音

    http://blog.csdn.net/cuoban/article/details/50552644

  7. [翻译] MSAlertController

    MSAlertController You can use AlertController in iOS7!! 你可以在iOS中使用AlertController了 MSAlertController ...

  8. APP中的 H5和原生页面如何分辨?

    一.APP内嵌H5和原生的区别 1.原生的页面运行速度快,比较流畅.H5页面相对原生的运行性能低,特别是一些动画效果有明显卡顿. 2.H5页面的很多交互都没有原生的好,比如弹层.输入时候的页面滑动 等 ...

  9. iOS真机命令(自动化测试)

    获取设备的的UDID idevice_id --list # 显示当前所连接设备的 udid instruments -s devices # 列出所有设备,包括真机.模拟器.mac idevicei ...

  10. 做 fzu oj 1003 简单的枚举

    暴力求解法---简单枚举 定义一个函数(函数的内容大概是包含了题目所给的限制条件),然后主函数就是通过循环进行枚举,枚举出可能的元素,带入函数中进行验证,如果符合函数所给的情况,则为其解.