任务概述
本次项目的首要工作是搭建数据库,首先就必须收集数据。
为此在网上寻找甲骨文-汉字和小篆-汉字的转换表,通过python对所得的数据进行处理,得到甲骨文-汉字和小篆-汉字的对比库。
获取甲骨文数据
1.获取数据的网址
http://jiaguwen.shufami.com/
2.图片提取及汉字甲骨文对照表
def transform(s_path, d_path):
# 使用正则表达式来查找图片
checkXO = r"/Type(?= */XObject)"
checkIM = r"/Subtype(?= */Image)"
# 打开pdf
pdf_doc = fitz.open(s_path)
# 图片计数
imgcount = 0
lenXREF = pdf_doc.xref_length()
# 遍历每一个对象
for i in range(1, lenXREF):
# 定义对象字符串
xref_text = pdf_doc.xref_object(i)
isXObject = re.search(checkXO, xref_text)
# 使用正则表达式查看是否是图片
isImage = re.search(checkIM, text)
# 如果不是对象也不是图片,则continue
if not is XObject or not isImage:
continue
imgcount += 1
# 根据索引生成图像
pix = fitz.Pixmap(doc, i)
import pdfplumber
total_charactor_list =[]
# 利用pdfplumber多个提取表格
with pdfplumber.open('duizhaobiao.pdf') as pdf:
#对每一页的pdf进行文字提取
for page in pdf.pages:
for table in page.extract_tables():
#所提取出来的字符可能是简体字,可能是None、空字符、英文字符
for character_list in table:
for character in character_list:
if character ==None or character=="":
pass
else:
#判断是不是简体字
if ord(character) not in range(65,90+1):
total_charactor_list.append(character)
获取小篆数据
获取网址同上
关键代码展示:
#模拟浏览器进行操作
etymologySearchChar =driver.find_element_by_id("etymologySearchChar")
etymologySearchChar.clear()
#对某个字进行输入
etymologySearchChar.send_keys(line[0])
etymologySearchButton=driver.find_element_by_id("etymologySearchButton")
etymologySearchButton.click()
time.sleep(5)
#找寻含有小篆字体的元素
background_image =driver.find_elements_by_xpath('//div[@data-target="#etymologyModal"]')
for i in range(len(background_image)):
time.sleep(0.1)
#确定存储路径
image =background_image[i]
final_path =path+"\\"+image.text+'.png'
#确定图片两个角的坐标
left = image.location['x']+5
top = image.location['y']
right = image.location['x'] + image.size['width']-2
bottom = image.location['y'] + image.size['height']-2
driver.save_screenshot(final_path)
#对图片进行截图
photo = Image.open(final_path)
photo = photo.crop((left, top, right, bottom))
#存储截图
photo.save(final_path)
最终数据集展示: