Python提取网页数据后导出表格并且数据转化为json


Python输出CSV文件

import sys
import csv
import cx_Oracle
connection = raw_input("Enter Oracle DB connection (uid/pwd@database) : ")
orcl = cx_Oracle.connect(connection)
curs = orcl.cursor()
printHeader = True # include column headers in each table output
sql = "select * from tab" # get a list of all tables
curs.execute(sql)
for row_data in curs:
if not row_data[0].startswith('BIN$'): # skip recycle bin tables
tableName = row_data[0]
# output each table content to a separate CSV file
csv_file_dest = tableName + ".csv"
outputFile = open(csv_file_dest,'w') # 'wb'
output = csv.writer(outputFile, dialect='excel')
sql = "select * from " + tableName
curs2 = orcl.cursor()
curs2.execute(sql)
if printHeader: # add column headers if requested
cols = []
for col in curs2.description:
cols.append(col[0])
output.writerow(cols)
for row_data in curs2: # add table rows
output.writerow(row_data)
outputFile.close()

for row, row_date in enumerate(data):
worksheet.write_row(row+1, 0, row_date)
print time.ctime()
cursor.close()
con.close()
workbook.close()

 

 

转化为json串

import sys
import cx_Oracle
import json


orcl = cx_Oracle.connect('xxx/xxx@orcl')

jsonData = []
curs2 = orcl.cursor()
sql = "select * from bctb where url='http://bc.58.com/zufang/30386873025067x.shtml'"
curs2.execute(sql)
i=0

for row in curs2:
result={}
result['nm'] = str(row[0])
result['dc'] = str(row[1])
result['iu'] = str(row[2])
result['it'] = str(row[3])
jsonData.append(result)
jsondatar = json.dumps(jsonData,ensure_ascii=False,indent=4)
print jsondatar

 

 

写入xls文件

 

import sys
import xlwt
import cx_Oracle

 

wbk=xlwt.Workbook()

sheet=wbk.add_sheet('foobar', cell_overwrite_ok=True)

sheet.write(0,0,‘QQ’)

wbk.save("foobar.xls")

 

 

合并成功版(能够输出json串和xls文件)

import sys
import xlwt
import cx_Oracle
import json

orcl = cx_Oracle.connect('xxx/xxx@orcl')
jsonData = []
curs2 = orcl.cursor()
sql = "select * from bctb "
curs2.execute(sql)
i=0
wbk=xlwt.Workbook()
sheet=wbk.add_sheet('foobar', cell_overwrite_ok=True)
for row in curs2:
result={}
result['url'] = row[0]
result['name'] =row[1]
result['jieshao'] = row[2]
result['diqu'] = row[3]
jsonData.append( result)
sheet.write(i,0,row[0])
sheet.write(i,1,row[1].decode('utf-8'))
sheet.write(i,2,row[2].decode('utf-8'))
sheet.write(i,3,row[3].decode('utf-8'))
i=i+1

wbk.save("foobar.xls")
jsondatar = json.dumps(jsonData, ensure_ascii=False,indent=4)
#对jsondata可以进行数组操作,但是对jsondatar不行


print jsondatar

打赏

免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2021 CODEPRJ.COM