csv 使用请求库保存数据

nc1teljy  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(145)

晚安,我试图从一个URL下载一个Excel表与请求库,但当我试图保存数据在表格模式,我从URL获得所有信息,但在HTML/文本代码,我不知道如何获得与相关信息的表:

URL = 'https://docs.google.com/spreadsheets/d/1PS2_yAvNVEuSY0gI8Nky73TQMcx_G1i18lm--jOGfAA/edit#gid=514147473'
response = requests.get(URL)

with open('out.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    for line in response.iter_lines():
      writer.writerow(csvheader)
      writer.writerow(line.decode('utf-8').split(','))

并获得一些类似于:

<!DOCTYPE html><html lang="en-US"><head><script nonce="JUuaJUl__6QGcoAgULSNpA">var DOCS_timing={}; DOCS_timing[\'pls\']=new Date().getTime();</script><meta property="og:title" content="Datos Argentina - Museos"><meta property="og:type" content="article"><meta property="og:site_name" content="Google Docs"><meta property="og:url" content="https://docs.google.com/spreadsheets/d/1PS2_yAvNVEuSY0gI8Nky73TQMcx_G1i18lm--jOGfAA/edit?usp=embed_facebook"><meta property="og:image" content="https://lh3.googleusercontent.com/N5mR8roa7WP4kn9xvBlt5U-OOKKkSkIGTofqsHXrk2iio25IlUBc-3V9qNJ8sg9Jqptk-mV4H1y7FQ=w1200-h630-p"><meta property="og:image:width"

信息是这样的:dataset from the URL

niwlg2el

niwlg2el1#

将URL从/edit更改为/export?format=csv

import requests

URL = "https://docs.google.com/spreadsheets/d/1PS2_yAvNVEuSY0gI8Nky73TQMcx_G1i18lm--jOGfAA/export?format=csv"

with open("out.csv", "wb") as f_out:
    f_out.write(requests.get(URL).content)

已保存out.csv(LibreOffice的屏幕截图):

相关问题