pandas to_html

如何将表格数据以图片的形式展现,主要目的则是为了防止爬虫。

为了解决这个问题,刚开始选择的是matplotlib.pyplot.table,但由于随着数据的字段长短不一,且matplotlib实际落地的过程中存在许许多多的坑,最终还是没有采用。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
 
 
df = pd.DataFrame(np.zeros((10,3)),columns=["c1","c2","c3"])
 
fig, axs = plt.subplots()
clust_data = df.values
collabel = df.columns
axs.axis('tight')
axs.axis('off')
the_table = axs.table(cellText=clust_data,colLabels=collabel,loc='center')
plt.show()

  

目前的解决方案:

1. 生成html table代码

2. chrome 屏幕大小调整后截屏

寻寻觅觅到头来发现pandas 有一个叫做to_html的方法,DataFrame数据流直接生成表单html。

截屏代码:

from selenium import webdriver

driver = webdriver.Chrome()
driver.set_window_size(1000, 680)
driver.get('file:///C:/Users/KC10/Desktop/data%20clearn/table.html')
driver.save_screenshot('table.png')
driver.quit()

  

  

posted @ 2019-01-25 15:42  家迪的家  阅读(5194)  评论(0编辑  收藏  举报