批量扫描ISBN
我使用的是Github开源的QrAndBarcodeScanner,需要的请自行下载。
打开设置中的连续扫描、保存扫描到的条形码到历史这两个功能。
导出扫描结果
将结果整理到txt
使用Python通过API批量获取信息
本例中使用的API为阿里云云市场的ISBN书号查询API,如使用其他API,请自行修改代码中的相关部分。
import urllib.request, urllib.parse, urllib.error, sys import ssl import json import tqdm # 定义输入和输出的文件名 input_file = "isbn_list.txt" output_file = "isbn_result.txt" # 定义API的相关参数 host = 'https://jmisbn.market.alicloudapi.com' path = '/isbn/query' method = 'POST' appcode = '替换为你的APPCODE' querys = '' bodys = {} url = host + path # 创建一个SSL上下文,忽略证书验证 ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE # 打开输入文件,按行读取ISBN号 with open(input_file, "r") as f_in: # 获取输入文件的总行数,作为进度条的最大值 total_lines = sum(1 for line in f_in) # 重新定位文件指针到开头 f_in.seek(0) # 打开输出文件,准备写入查询结果 with open(output_file, "w", encoding="utf-8") as f_out: # 指定输出文件的编码为utf-8,避免出现编码错误 # 创建一个tqdm对象,用来显示进度条 pbar = tqdm.tqdm(total=total_lines, desc="查询进度") # 对于输入文件中的每一行 for line in f_in: # 去掉行尾的换行符 line = line.strip() # 如果行不为空 if line: # 把行作为ISBN号 bodys['isbn'] = line # 编码请求参数 post_data = urllib.parse.urlencode(bodys) # 创建请求对象 request = urllib.request.Request(url, post_data.encode('utf-8')) # 添加请求头 request.add_header('Authorization', 'APPCODE ' + appcode) # 根据API的要求,定义相对应的Content-Type request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8') # 发送请求,获取响应 response = urllib.request.urlopen(request, context=ctx) # 读取响应内容 content = response.read() # 如果响应内容不为空 if content: # 把响应内容解码为字符串,并转换为字典 data = json.loads(content.decode('utf-8')) # 从字典中获取code值,如果没有则返回0 code = data.get('code', 0) # 如果code值为200,表示成功 if code == 200: # 从字典中获取title、author、publisher的值,如果没有则返回空字符串 title = data.get('data', {}).get('details', [{}])[0].get('title', '') author = data.get('data', {}).get('details', [{}])[0].get('author', '') publisher = data.get('data', {}).get('details', [{}])[0].get('publisher', '') # 用逗号分隔这三个值,并写入输出文件,加上换行符 f_out.write(','.join([title, author, publisher]) + "\n") # 否则,表示失败 else: # 写入ERROR,并加上换行符 f_out.write("ERROR\n") # 更新进度条,增加一个计数 pbar.update(1) # 关闭进度条 pbar.close()b
本代码只能查询ISBN为9787开头的内地书籍,其他的会报错。