python3爬虫-通过requests获取拉钩职位信息
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获取拉钩职位信息的更多相关文章
- python3爬虫-通过requests获取安居客房屋信息
import requests from fake_useragent import UserAgent from lxml import etree from http import cookiej ...
- 21天打造分布式爬虫-Selenium爬取拉钩职位信息(六)
6.1.爬取第一页的职位信息 第一页职位信息 from selenium import webdriver from lxml import etree import re import time c ...
- python3爬虫-通过selenium登陆拉钩,爬取职位信息
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from se ...
- python3爬虫抓取智联招聘职位信息代码
上代码,有问题欢迎留言指出. # -*- coding: utf-8 -*- """ Created on Tue Aug 7 20:41:09 2018 @author ...
- ruby 爬虫爬取拉钩网职位信息,产生词云报告
思路:1.获取拉勾网搜索到职位的页数 2.调用接口获取职位id 3.根据职位id访问页面,匹配出关键字 url访问采用unirest,由于拉钩反爬虫,短时间内频繁访问会被限制访问,所以没有采用多线程, ...
- python3爬虫-通过requests爬取图虫网
import requests from fake_useragent import UserAgent from requests.exceptions import Timeout from ur ...
- 通俗易懂的分析如何用Python实现一只小爬虫,爬取拉勾网的职位信息
源代码:https://github.com/nnngu/LagouSpider 效果预览 思路 1.首先我们打开拉勾网,并搜索"java",显示出来的职位信息就是我们的目标. 2 ...
- python3 requests 获取 拉勾工作数据
#-*- coding:utf-8 -*- __author__ = "carry" import requests,json for x in range(1, 15): url ...
- python3爬虫-使用requests爬取起点小说
import requests from lxml import etree from urllib import parse import os, time def get_page_html(ur ...
随机推荐
- 【javascript】javascript设计模式mixin模式
概述: Mixin是JavaScript中用的最普遍的模式,几乎所有流行类库都会有Mixin的实现.任意一个对象的全部或部分属性拷贝到另一个对象上. 一 .混合对象 二 .混合类
- flask代码统计作业
用户表: create table userInfo( id int not null unique auto_increment, name )not null, password ) not nu ...
- PLSQL Developer V10 注册码
PLSQL Developer V10 注册码(V10.0.3.1701可以使用)注册时间至:9999/12/31 License Number:999 Product Code:ljkfuhjpcc ...
- [NCH 1, 3]
Preview: 1. Implement strStr() O(m*n): class Solution { public: int strStr(string haystack,string ne ...
- HTML5离线存储之webstorage
html5在引入webStorage之前,主要用cookies. html5的webstorage 分两种:LocalStorage 和SessionStorage,两者的差别主要在生命周期不同. 1 ...
- DDD(领域驱动设计)总结
基本概念: 领域驱动设计(简称 ddd)概念来源于2004年著名建模专家eric evans发表的他最具影响力的书籍:<domain-driven design –tackling comple ...
- rsync- sersync -inotify
Rsync简介 Rsync是一款优秀的.快速的.多功能的本地或远程数据镜像同步备份工具.适用于unix/linux/windows等多种平台 从软件的名称Rsync(Remote Rynhroniza ...
- Python语言特性
1 Python的函数参数传递 看两个例子: a = 1 def fun(a): a = 2 fun(a) print a # 1 a = [] def fun(a): a.append(1) fun ...
- 低版本兼容使用Fragment (转)
Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持.以前的版本必须用兼容模式开发,本人在网上找了大量资料,终于找到些线索正常运行于2.1版本的安卓系统.现在浅说一下兼容版本使用Fr ...
- Apache Jemeter 开发插件
为什么选择使用JMeter 当被问到这个问题的时候,也许你会在脑海里产生很多的理由,比如: Apache基金会下的开源项目,没有版权问题: 为数不多的还在持续更新的开源性能自动化测试工具: 支持协议丰 ...