gpt4 book ai didi

python - 为什么这个 python 脚本可以在我的本地计算机上运行,​​但不能在 Heroku 上运行?

转载 作者:行者123 更新时间:2023-12-01 02:39:08 25 4
gpt4 key购买 nike

那里。我正在构建一个简单的抓取工具。这是我的代码。

from bs4 import BeautifulSoup
import requests
from lxml import html
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import datetime

scope = ['https://spreadsheets.google.com/feeds']

credentials = ServiceAccountCredentials.from_json_keyfile_name('Programming
4 Marketers-File-goes-here.json', scope)

site = 'http://nathanbarry.com/authority/'
hdr = {'User-Agent':'Mozilla/5.0'}
req = requests.get(site, headers=hdr)

soup = BeautifulSoup(req.content)

def getFullPrice(soup):
divs = soup.find_all('div', id='complete-package')
price = ""
for i in divs:
price = i.a
completePrice = (str(price).split('$',1)[1]).split('<', 1)[0]
return completePrice


def getVideoPrice(soup):
divs = soup.find_all('div', id='video-package')
price = ""
for i in divs:
price = i.a
videoPrice = (str(price).split('$',1)[1]).split('<', 1)[0]
return videoPrice

fullPrice = getFullPrice(soup)
videoPrice = getVideoPrice(soup)
date = datetime.date.today()

gc = gspread.authorize(credentials)
wks = gc.open("Authority Tracking").sheet1

row = len(wks.col_values(1))+1

wks.update_cell(row, 1, date)
wks.update_cell(row, 2, fullPrice)
wks.update_cell(row, 3, videoPrice)

该脚本在我的本地计算机上运行。但是,当我将其作为应用程序的一部分部署到 Heroku 并尝试运行它时,出现以下错误:

回溯(最近一次调用最后一次): 文件“/app/.heroku/python/lib/python3.6/site-packages/gspread/client.py”,第 219 行,在 put_feed 中 r = self.session.put(url, 数据, headers=headers) 文件“/app/.heroku/python/lib/python3.6/site-packages/gspread/httpsession.py”,第 82 行,输入 返回 self.request('PUT', url, params=params, data=data, **kwargs) 文件“/app/.heroku/python/lib/python3.6/site-packages/gspread/httpsession.py”,第 69 行,根据请求 响应.状态代码,响应.内容))gspread.exceptions.RequestError: (400, "400: b'cell_id 的查询参数值无效。'")

在处理上述异常的过程中,又发生了一个异常:

回溯(最近一次调用最后一次): 文件“AuthorityScraper.py”,第 44 行,位于 wks.update_cell(行, 1, 日期) 文件“/app/.heroku/python/lib/python3.6/site-packages/gspread/models.py”,第 517 行,在 update_cell 中 self.client.put_feed(uri, ElementTree.tostring(feed)) 文件“/app/.heroku/python/lib/python3.6/site-packages/gspread/client.py”,第 221 行,在 put_feed 中 如果 ex[0] == 403:类型错误:“RequestError”对象不支持索引

您认为什么可能导致此错误?您对我如何解决这个问题有什么建议吗?

最佳答案

发生了一些事情:

1) Google Sheets API 返回错误:“cell_id 的查询参数值无效”:

gspread.exceptions.RequestError: (400, "400: b'Invalid query parameter value for cell_id.'")

2) gspread 中的错误导致收到错误后出现异常:

TypeError: 'RequestError' object does not support indexing

Python 3 从 BaseException 中删除了 __getitem__,这是 gspread 错误处理所依赖的。这并不重要,因为无论如何它都会引发 UpdateCellError 异常。

我的猜测是您向 update_cell 传递了无效的行号。例如,向脚本中添加一些调试日志记录以显示它正在尝试更新哪一行会很有帮助。

最好从零行的工作表开始,然后使用 append_row 代替。然而,似乎确实有一个突出的issuegspreadappend_row 中,它实际上可能与您遇到的问题相同。

关于python - 为什么这个 python 脚本可以在我的本地计算机上运行,​​但不能在 Heroku 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45910214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com