完美解决python爬取网页数据导json然后输出到excel.xlsx

代码:

xlwings 类似于excel中的VBA,很好用

import json
import xlwt
import xlwings as xw

js = [{"time": 1605868916, "kw": "携程"},
        {"time": 1605868992, "kw": "丽江"},
        {"time": 1605869065, "kw": "c6179"},
        {"time": 1605869267, "kw": "丽江景点"},
        {"time": 1605869312, "kw": "女童误服近40粒降压药不幸身亡"},
        {"time": 1606112160, "kw": "python爬虫请求头"},
        {"time": 1606112420, "kw": "useragent"},
        {"time": 1606112494, "kw": "手机useragent"},
        {"time": 1606112849, "kw": "谷歌浏览器useragent"},
        {"time": 1606114944, "kw": "百度贴吧"}]

with open('js.json', 'w', encoding='utf-8') as f:
    json.dump(js, f, indent=1, ensure_ascii=False)

#读取jon文件
with open('js.json','r',encoding='utf-8') as f:
    data=json.load(f)
    # print(len(data))

# 循环插入Excel表中
app=xw.App(visible=True,add_book=False)
new_workbook=xw.Book()
new_worksheet=new_workbook.sheets.add('test')

for i in range(len(data)):
    time=data[i]["time"]
    kw=data[i]["kw"]
    new_worksheet.cells[i,0].value= data[i]["time"]
    new_worksheet.cells[i,1].value=data[i]["kw"]
    i=i+1
    print(i)
# new_worksheet['A1'].value= time
# new_worksheet['B1'].value=kw
# new_worksheet.range('A2').options(transpose=True).value=time
# new_worksheet.range('A1').options(transpose=True).value=time

new_worksheet.autofit()
new_workbook.save('新建文件夹.xlsx')
new_workbook.close()
app.quit()


'''
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('Worksheet')

# 写入excel参数对应 行, 列, 值
worksheet.write(0, 0, label='time')
worksheet.write(0, 1, label='kw')

# 循环插入Excel表中
for i in range(len(data)):
    time=data[i]["time"]
    kw=data[i]["kw"]
    worksheet.write(i+1,0,label=time)
    worksheet.write(i+1,1,label=kw)
workbook.save('Excel2.xls')
'''

json 格式:

movie_data = [{"time": 1605868916, "kw": "程"},
        {"time": 1605868992, "kw": "丽"},
        {"time": 1605869065, "kw": "c61"}]
movie_data1 = [{"time": 1605868916, "kw": "程"},
        {"time": 1605868992, "kw": "丽"},
        {"time": 1605869065, "kw": "c61"}]
# movie_data.append( {"time": 1605869267, "kw": "景点"})
movie_data.append(movie_data1)
with open('movie_data.json', 'w', encoding='utf-8') as f:
    json.dump(movie_data, f, indent=1, ensure_ascii=False)
import json
import xlwt

person_data=[]#定义数组
#数组赋值
person_data.append({
    '序号':1,'name':"小王",'age':12})
#输入json
with open('person_data.json','w',encoding='utf-8') as f:
    json.dump(person_data,f,indent=1,ensure_ascii=False)

# 创建一个excel对象
book = xlwt.Workbook()
sheet = book.add_sheet('Sheet1',cell_overwrite_ok=True) # 添加一个sheet页
#设置表头
title=["序号","name","age"]
for i in range(len(title)):
    sheet.write(0,i,title[i])
#读取jon文件
with open('person_data.json','r') as f:
    data=json.load(f)

for i in range(len(title)):
        sheet.write(0,i,title[i])

for line in data:
    print('line',line)
#     sheet.write(int(line), 0, line)

代码:

import xlwt
import json

loc= [{"time": 1605868916, "kw": "程"},
        {"time": 1605868992, "kw": "丽"},
        {"time": 1605869065, "kw": "c61"},
        {"time": 1605869267, "kw": "景点"},
        {"time": 1605869312, "kw": "女童误服"}]

# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('Worksheet')

# 写入excel参数对应 行, 列, 值
worksheet.write(0, 0, label='time')
worksheet.write(0, 1, label='kw')

# 循环插入Excel表中
for i in range(len(loc)):
    time = loc[i]["time"]
    kw = loc[i]["kw"]
    worksheet.write(i + 1, 0, label=time)
    worksheet.write(i + 1, 1, label=kw)
# 保存,文件自动创建到项目目录下
workbook.save('Excel.xls')

你可能感兴趣的:(python,python,json,爬虫)