#!/usr/bin/env python
#coding:utf-8
'''
python发展方向:
1.web开发
2.自动化运维开发,测试
3.大数据
4.人工自能
思想介绍:
1.如何爬去拉钩网的数据
爬虫:从网页上面采集数据的过程
1.获取页面的html源码
2.json: 数据交换 dumps(json格式化字符串将dict转化成str) loads(json格式字符串转化成 dict)
2.对数据进行处理,存入excel表格。
1.创建excel表格,并且加入表头部信息
2.写入数据
'''
import requests,xlwt,json
pn = 1
items = []
def get_content(pn):
url = 'https://www.lagou.com/jobs/positionAjax.json?px=default&needAddtionalResult=false'
data = {'first':'true',
'pn':pn,
'kd':'python'}
html = requests.post(url,data).text
html = json.loads(html)
# print html
for i in range(14):
item = []
item.append(html['content']['positionResult']['result'][i]['positionName']) #招聘职位
item.append(html['content']['positionResult']['result'][i]['companyFullName']) #公司名称
item.append(html['content']['positionResult']['result'][i]['salary']) #薪资
item.append(html['content']['positionResult']['result'][i]['city']) #地点
item.append(html['content']['positionResult']['result'][i]['companyLabelList']) #福利
item.append(html['content']['positionResult']['result'][i]['secondType']) #职位名称
items.append(item)
#print items
return items
#创建excel表格
def excel_write(items):
newTable = 'text.xls' #表的名称
wb = xlwt.Workbook(encoding='utf-8') #创建excel文件,设置编码
ws = wb.add_sheet('test1')
headData = ['招聘职位','公司名称','薪资','地点','福利','职位名称']
for hd in range(0,6):
ws.write(0,hd,headData[hd],xlwt.easyxf('font: bold on'))
#wb.save(newTable)
#把爬虫获取取到的数据写入excel表格
index = 1
for item in items:
for i in range(0,6):
#print item[i] #每一行的信息
ws.write(index,i,item[i])
index += 1
wb.save(newTable)
if __name__ == '__main__':
for pn in range(1,5):
res = get_content(pn)
excel_write(res)