2017-08-27 8 views
1

があります。私は単純なスクレイピングツールを構築しています。ここに私が持っているコードがあります。なぜこのpythonスクリプトはローカルマシンでは動作しますが、Herokuでは動作しませんか?

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、data、headers = headers) ファイル" /app/.heroku/python/lib /python3.6/site-packages/gspread/httpsession.py "、行82、put return self.request( 'PUT'、url、params = params、data = data、** kwargs) ファイル"/app /.heroku/python/lib/python3.6/site-packages/gspread/httpsession.py "、69行目、リクエスト中 response.status_code、response.content) 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の場合: TypeError: 'RequestError'オブジェクトはインデックス付けをサポートしていません

このエラーの原因は何でしょうか?私はそれをどのように修正することができますか?

答えて

2

起こって物事のカップルがあります:

1)Googleスプレッドシート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を使用する方がよい場合があります。しかしはgspreadにあり、append_rowと表示されている場合があります。実際に実行している問題と同じかもしれません。

関連する問題