Python:将输出导出到Google表格

问题描述

我编写了这个网络抓取程序,该程序从IG市场提取26个外汇对的零售交易情绪数据。

控制台中的输出如下:

AUD-CAD: 64% of client accounts are short.
AUD-CHF: 54% of client accounts are long.
AUD-JPY: 60% of client accounts are short.
AUD-NZD: 60% of client accounts are long.
AUD-USD: 53% of client accounts are short.
CAD-CHF: 56% of client accounts are long.
CAD-JPY: 56% of client accounts are long.
CHF-JPY: 68% of client accounts are short.
EUR-AUD: 68% of client accounts are long.
EUR-CAD: 65% of client accounts are short.
EUR-CHF: 66% of client accounts are long.
EUR-GBP: 53% of client accounts are short.
EUR-JPY: 57% of client accounts are short.
EUR-NZD: 55% of client accounts are long.
EUR-USD: 54% of client accounts are short.
GBP-AUD: 73% of client accounts are long.
GBP-CAD: 66% of client accounts are long.
GBP-CHF: 63% of client accounts are long.
GBP-JPY: 52% of client accounts are short.
GBP-NZD: 57% of client accounts are long.
GBP-USD: 59% of client accounts are short.
SPOT-FX-NZDCAD: 68% of client accounts are short.
NZD-CHF: 59% of client accounts are short.
NZD-JPY: 57% of client accounts are short.
NZD-USD: 72% of client accounts are short.
USD-CAD: 69% of client accounts are long.
USD-CHF: 79% of client accounts are long.
USD-JPY: 58% of client accounts are long.

我想将此数据导出到名为“ GsheetTest”的Google表格中,但是我被卡住了, 我不知道该怎么做。

Google API已启用。我创建了凭据,获得了服务帐户json密钥。

我可以使用pygsheets和panda数据框向此Google表格文件“ GsheetTest”中写入简单的文本。

这是代码:

import bs4,requests

def getIGsentiment(pairUrl):
    res = requests.get(pairUrl)
    res.raise_for_status()'

soup = bs4.BeautifulSoup(res.text,'html.parser')
elems = soup.select('.price-ticket__sentiment')
return elems[0].get_text(" ",strip = True)


pair_list = ['aud-cad','aud-chf','aud-jpy','aud-nzd','aud-usd','cad-chf','cad-jpy','chf-jpy','eur-aud','eur-cad','eur-chf','eur-gbp','eur-jpy','eur-nzd','eur-usd','gbp-aud','gbp-cad','gbp-chf','gbp-jpy','gbp-nzd','gbp-usd','spot-fx-nzdcad','nzd-chf','nzd-jpy','nzd-usd','usd-cad','usd-chf','usd-jpy']
for i in range(len(pair_list)):
    retail_positions = getIGsentiment('https://www.ig.com/us/forex/markets-forex/ +(pair_list[i]))
    pair = pair_list[i]
    print(pair.upper() +': ' + retail_positions[0:32].rstrip() + '.')

解决方法

如果您有字符串列表,并且想要将每个字符串写入单个单元格。我想这就是你想要的。

import pygsheets
import numpy as np

pair_data = []  # data fro your code

gc = pygsheets.authorize()

# Open spreadsheet and then worksheet
sh = gc.open('GsheetTest')
wks = sh.sheet1
wks.wks.update_values('A1',pair_data)
,

首先在浏览器中打开json文件并找到电子邮件地址,现在与该电子邮件共享电子表格并进行编辑,然后使用以下代码。

#Import these libraries or pip install if not installed already
from oauth2client.service_account import ServiceAccountCredentials
import gspread
import bs4,requests
def getIGsentiment(pairUrl):
    res = requests.get(pairUrl)
    res.raise_for_status()'

soup = bs4.BeautifulSoup(res.text,'html.parser')
elems = soup.select('.price-ticket__sentiment')
return elems[0].get_text(" ",strip = True)


pair_list = ['aud-cad','aud-chf','aud-jpy','aud-nzd','aud-usd','cad-chf','cad-jpy','chf-jpy','eur-aud','eur-cad','eur-chf','eur-gbp','eur-jpy','eur-nzd','eur-usd','gbp-aud','gbp-cad','gbp-chf','gbp-jpy','gbp-nzd','gbp-usd','spot-fx-nzdcad','nzd-chf','nzd-jpy','nzd-usd','usd-cad','usd-chf','usd-jpy']


#now you need auth and set scope 

scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('path-to-your-json.json',scope)
#Auth
gc = gspread.authorize(creds)
sh = gc.open('Spread Sheet name You want to open')
worksheet = sh.add_worksheet('sheet to be added name',int(rows),int(columns))


for i in range(len(pair_list)):
    retail_positions = getIGsentiment('https://www.ig.com/us/forex/markets-forex/ +(pair_list[i]))
    pair = pair_list[i]
    foo = pair.upper() +': ' + retail_positions[0:32].rstrip() + '.'
    worksheet.insert_row(foo,1)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...