Crawler:爬虫之基于https+parse库实现爬取国内某知名招聘网上海、北京对于区块链职位求职信息x
来源:考研 发布时间:2020-08-26 点击:
Crawler :爬虫之基于 https+parse 库实现爬取国内某知名招聘网上海、北京关于区块链职位的求职信息 Crawler:爬虫之基于 https+parse 库实现爬取国内某知名招聘网上海、北京关于区块链职位的求职信息
目录 目录 输出结果 1、上海区块链职位 2、北京区块链职位 设计思路 核心代码
输出结果 1 、上海区块链职位 国内某知名招聘网上海关于区块链职位的求职信息分析图
2 、北京区块链职位 国内某知名招聘网北京关于区块链职位的求职信息分析图
设计思路 Python 实现爬取国内某知名招聘网上海、北京关于区块链职位的求职信息
核心代码
from https import Http
from parse import Parse
from setting import headers
from setting import cookies
import time
import logging
import codecs
#logging.basicConfig 函数为日志系统做基本配置:Python 写比较大型的程序,用日志系统会方便发现错误。
logging.basicConfig(level=logging.ERROR,
format="%(asctime)s Process%(process)d:%(thread)d %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
filename="diary.log",
filemode="a")
def getInfo(url, para): #输入网址、参数实现获取网页上的内容
generalHttp = Http()
htmlCode = generalHttp.post(url, para=para, headers=headers, cookies=cookies)
generalParse = Parse(htmlCode)
pageCount = generalParse.parsePage()
info = []
def getInfoDetail(generalParse):
info = generalParse.parseInfo()
return info
def processInfo(info, para): #实现将爬取的网页内容存入到 xls 表格文件
logging.error("Process start")
try:
title = "公司名称\t 公司类型\t 融资阶段\t 标签\t 公司规模\t 公司所在地\t 职位类型\t 学历要求\t 福利\t 薪资\t 工作经验\t 发布时间\n"
file = codecs.open("%s 市有关区块链的职位信息表.xls" % para["city"], "w", "utf-8")
file.write(title) #file.write 函数先将 xls 表格的列名写入 xls 文件内
for p in info: #for 循环依次将十二个字段的内容写入 xls 文件
line = str(p["companyName"]) + "\t" + str(p["companyType"]) + "\t" + str(p["companyStage"]) + "\t" + \
str(p["companyLabel"]) + "\t" + str(p["companySize"]) + "\t" + str(p["companyDistrict"]) + "\t" + \
str(p["positionType"]) + "\t" + str(p["positionEducation"]) + "\t" + str(
p["positionAdvantage"]) + "\t" + \
str(p["positionSalary"]) + "\t" + str(p["positionWorkYear"]) + "\t" + str(p["createTime"])+"\n"
file.write(line) #最后将 line 写入 xls 文件
file.close() #file.close()关闭 xls 文件
return True
except Exception as e:
print(e)
return None
def main(url, para): #函数开始进行爬取网址
logging.error("Main start")
if url:
info = getInfo(url, para)
flag = processInfo(info, para)
return flag
else:
return None
if __name__ == "__main__":
kdList = [u"区块链"]
cityList = [u"上海", u"北京"]
url = "https://www.lagou.com/jobs/positionAjax.json"
for city in cityList:
print("现在开始爬取%s" % city)
para = {"first": "true", "pn": "1", "kd": kdList[0], "city": ci
ty} #{,页数,城市}
flag = main(url, para)
if flag: #if 人性化输出是否爬取成功
print("%s 爬取成功!" % city)
else:
print("%s 爬取失败!" % city)
推荐访问:爬虫 区块 招聘网