在线表格已成为趋势,传统的表格导入导出已经不能满足现在企业的发展。
必须支持在线表格的导入,只需要输入在线表格的链接,就能将数据读取出来。
而且相比相传表格,能够将图片信息一块读取。在线表格中的图片也是图片链接形式。
附上源码,这里我只打印了每一行数据的数组形式,大家根据各自需求复用代码。
import requests,random from bs4 import BeautifulSoup #获得静态的界面 def get_static_html(site_url): print('开始加载', site_url, '静态页面') headers_list = [ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0 ', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' ] headers = { 'user-agent': headers_list[random.randint(0,len(headers_list))-1], 'Connection': 'keep - alive' } try: resp = requests.get(site_url, headers=headers) except Exception as inst: print(inst) requests.packages.urllib3.disable_warnings() resp = requests.get(site_url, headers=headers,verify=False) soup = BeautifulSoup(resp.text, 'html.parser') return soup if __name__ == '__main__': #谷歌在线表格链接,需要保证你的链接所有人可见 url = 'https://docs.google.com/spreadsheets/d/1q8_tv8Hu0cReHPsUkvvwWa4G8QmsfqLWkIo3s1WJuHk/edit#gid=0' #开始一系列的处理 soup = get_static_html(url) tab_tr_arr = soup.find_all('tr') for tab_tr in tab_tr_arr: content = [] s0_tr_th_arr = tab_tr.select('.s0') s1_tr_th_arr = tab_tr.select('.s1') if len(s0_tr_th_arr) != 0: for tr_th in s0_tr_th_arr: content.append(tr_th.text) if len(s1_tr_th_arr) != 0: for tr_th in s1_tr_th_arr: img_src = tr_th.select_one('img').attrs['src'] content.append(img_src.split('=')[0]) #输出每行的数据 if len(content) != 0: print(content)
来源:https://www.cnblogs.com/lelexiu/p/10983748.html