温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Python怎么实现自动提取并收集信息

发布时间:2021-05-18 15:48:15 来源:亿速云 阅读:249 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关使用Python怎么实现自动提取并收集信息,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

代码实现

1.导入需要的库,包括百度的api接口跟cv2图像截图图库

import cv2 from aip import AipOcr # 读取图片,利用imshow显示图片 pic = cv2.imread(r'Y:\cut\img1.png') pic = cv2.resize(pic,None,fx = 0.5, fy = 0.5) cv2.imshow('img',pic) cv2.waitKey(0)

2.截取图片,获取需要的信息,包括以下信息

  • 时间Time

  • 商家business

  • 商品goods

  • 价格money

  • 单号num

# 删除不必要的部分 img = pic[210:500, 100:580] # 截取各部分的文字 time = pic[400:430, 100:580] business = pic[370:400, 100:580] goods = pic[350:380, 100:580] money = pic[210:300, 100:580] num = pic[460:500, 100:580] # 查看截取的部分是否合适 gener_name = ['time','business','goods','money','num'] excel_data = {} pd_columns = ["a","b","c","d","e"] # 标题

使用Python怎么实现自动提取并收集信息
使用Python怎么实现自动提取并收集信息
使用Python怎么实现自动提取并收集信息
使用Python怎么实现自动提取并收集信息

3.定义函数将截取好的图片另存到文件夹

def shotcut_image(args):     for index in gener:         cv2.imwrite('image/{}.png'.format(args), img)

4.调用百度api接口,实现文字识别

# 导入api AppID = '24177719' API_Key = 'p8skmRYfHGoVGR4UU03Q5jiM' Secret_Key = 'dyM0tzSILBZu9CFqZ7IkjWwECGaws4xo' cilent = AipOcr(AppID,API_Key,Secret_Key) def get_words(img_name):     with open('image/{}.png'.format(img_name), 'rb') as f:         result = cilent.basicAccurate(f.read())         return result

5.最后将信息转为Dataframe,利用pandas的to_exccel功能,将数据放到excel里面

def convert_to_dataframe(words):     # 构建dataframe     result = words['words_result']     for word in result:         excel_data.setdefault('a', []).append(word['words']) # 将所有words读取后,取出语句存入excel def convert_to_excel():     frame = DataFrame(excel_data, columns=pd_columns)     # todo 表头需要额外处理,这里指定不设置表头     frame.to_excel('out.xls',index=False, header=False)

使用Python怎么实现自动提取并收集信息

Python主要用来做什么

Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。

以上就是使用Python怎么实现自动提取并收集信息,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI