我的脚本在谷歌应用引擎的本地主机上运行得很好,但在部署脚本时,在云上显示了以下错误(appspot.com):
“错误:服务器错误
服务器遇到错误,无法完成您的请求。
请在30秒后重试。“
下面是我的代码:
import webapp2
import sys
sys.path.insert(0, 'libs')
import requests
from bs4 import *
import re
import smtplib
from google.appengine.api import urlfetch
from google.appengine import runtime
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write("hello")
#urlfetch.set_default_fetch_deadline(60)
def spider():
count = 1
href = 'www.example.com'
while count <= 2:
new_url = href
new_source_code = urlfetch.fetch(new_url, deadline=60)
new_plain_text = new_source_code.content
new_soup = BeautifulSoup(new_plain_text)
for new_link in new_soup.find_all('table'):
for new_link1 in new_link.find_all('a'):
new_href = 'www.example.com' + new_link1.get('href')
new1_url = new_href
new1_source_code = urlfetch.fetch(new1_url, deadline=60)
new1_plain_text = new1_source_code.content
new1_soup = BeautifulSoup(new1_plain_text)
for new1_link in new1_soup.find_all('tbody'):
for new1_link1 in new1_link.find_all('a', attrs={'class': 'title'}):
new1_title = new1_link1.string
new1_title = new1_title.strip()
new1_href = 'www.example.com' + new1_link1.get('href')
self.response.write(new1_title)
self.response.write(new1_href)
count = count + 1
spider()
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
我只想通过抓取来打印url,在部署后,我可以在本地主机上看到url,但在应用程序引擎上看不到,这会显示错误。
发布于 2014-10-26 23:44:48
弹性伸缩App Engine模块,截止时间为60秒。在您的示例代码中,您有两个URL Fetch请求,每个请求都在一个循环中,并且每个请求的截止时间都是60秒。假设您不是在基础伸缩或手动伸缩实例上运行,您可能会在60秒后发现此异常。即使在远程主机上超时一次,也会导致您超过前端的截止日期。
This page会为您提供不同实例伸缩类型的截止日期。
但是,您可能希望使用任务队列来帮助将工作分解为可管理的、“可重试的”块。
发布于 2014-10-26 23:51:59
Google App Engine中的每个请求都有60秒的最大硬限制,因此对于任何超过60秒的请求,您都将获得DeadlineExceededError
。
如果您预先知道请求将花费更多时间,那么您将不得不使用Tasks API,其中您可以运行长达10分钟的操作。最后,如果你想要更长的东西,看看Backends API,你可以运行长达24小时的东西。
https://stackoverflow.com/questions/26574238
复制相似问题