用表格形式保存文档 xlwt

#  - * _- coding:utf-8-*-

import requests
import json #转成字典形式
import xlwt
import sys #转码
reload(sys)
sys.setdefaultencoding('utf-8')
def Init(skinname): url="https://www.lagou.com/jobs/list_python?city=%E5%85%A8%E5%9B%BD&cl=false&fromSearch=true&labelWords=&suginput="
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0'}
totalpage=1
workbook = xlwt.Workbook()
for page in range(totalpage):
page += 1
c_url='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false' #循环页数需要抓包
data ={'first':'false',
'pn':page,
'kd':skinname}
html=requests.post(c_url,data,headers) #页面是用post请求
items=json.loads(html.text) #转换成字典形式
Data=readpage(items)
workbook = xlwt.Workbook()
sheetname = 'Sheet'
booksheet =workbook.add_sheet(sheetname) for i,row in enumerate (Data):
for j,col in enumerate(row):
booksheet.write(i,j,col)
xlsname='python.xls'
workbook.save(xlsname)
def readpage(response): #定义一个新的函数取所要的数据
page_lists=[]
page_list=[]
for i in range(14):
page_list.append(response['content']['positionResult']['result'][i]['salary'])
page_list.append(response['content']['positionResult']['result'][i]['workYear'])
page_list.append(response['content']['positionResult']['result'][i]['companyFullName'])
page_lists.append(page_list)
return page_lists
if __name__ =='__main__': #查找名字
Init('python')

用表格形式保存文档 xlwt

上一篇:MarkDown笔记


下一篇:tomcat密码的坑