。我正在構建一個簡單的抓取工具。這是我的代碼。爲什麼這個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行,放入 返回self.request('PUT',url,params = params,data = data,kwargs) File」/ app /.heroku/python/lib/python3.6/site-packages/gspread/httpsession.py「,第69行,請求 response.status_code,response.content)) gspread.exceptions.RequestError:(400,」400 :b'無效的查詢參數。爲單元ID值'「)
在處理上述異常,另一個異常:
回溯(最後最近一次調用): 文件 」AuthorityScraper.py「,第44行,在 wks.update_cell (row,1,date) 文件「/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'對象不支持索引
您認爲可能導致此錯誤的是什麼?你有什麼建議可以解決它嗎?