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. 当react 项目使用px2rem

    参考资料:http://easywork.xin/2018/09/02/react-2/ 我拿到的设计图 是  375px //配置px2rem px2rem({remUnit: 37.5})   在 ...

  2. Angular1.x directive(指令里的)的compile,pre-link,post-link,link,transclude

    The nitty-gritty of compile and link functions inside AngularJS directives  The nitty-gritty of comp ...

  3. Androidpdf

    https://www.jb51.net/article/110238.htm https://blog.csdn.net/u010046908/article/details/53927157 &l ...

  4. select 时进行update的操作,在高并发下引起死锁

    场景:当用户查看帖子详情时,把帖子的阅读量:ReadCount+1 select title,content,readcount from post where id='xxxx'   --根据主键查 ...

  5. Mysql学习---使用Python执行存储过程

    使用Python执行存储过程 使用Python执行存储过程[2部分]: 1.执行存储过程,获取存储过程的结果集  2.将返回值设置给了  @_存储过程名_序号 = #!/usr/bin/env pyt ...

  6. winform中webBrowser模拟网页操作中遇到的问题

    我们通过网页上传一些特殊数据的时候,由于必填项众多,数量量大的时候,会发现工作相当繁琐,前段时间做了一个winform内嵌webBrowser模拟网页上传文档的小工具,发现了许多问题,总结一下: 先说 ...

  7. 云端办公是 Office系统的未来方向么 ?

    云端办公是 Office系统的未来方向么 ? 话说随着互联网,HTML技术,云计算等技术的发展,越来越多的应用已经迁移到云端, 以我们熟悉的电脑游戏为例,从单机游戏,到网络游戏,再到网页游戏,基本就是 ...

  8. UML设计--人月神教

    任务分配 用例图 类图 活动图 状态图 使用工具 所有图都是用VISO编辑出来的,因为VISO是比较经典工具,也是学校电脑自带的.....

  9. 7、Node.js EventEmitter

    #######################################################################################介绍Node.js Eve ...

  10. BZOJ 1012 最大数maxnumber 线段树

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1012 题目大意: 见链接 思路: 直接用线段树模拟一下就可以了. #include&l ...