在我的RequestHandler子类中,我试图获取网址范围:
class GetStats(webapp2.RequestHandler):
def post(self):
lastpage = 50
for page in range(1, lastpage):
tmpurl = url + str(page)
response = urllib2.urlopen(tmpurl, timeout=5)
html = response.read()
# some parsing html
heap.append(result_of_parsing)
self.response.write(heap)
但是它可以使用约30个网址(页面加载时间很长,但是可以)。如果超过30,我会得到一个错误:
错误:服务器错误
服务器遇到错误,无法完成您的请求。
请在30秒后重试。
有什么方法可以获取很多网址吗?可能更理想或更合适?多达几百页?
更新:
我正在使用BeautifulSoup来解析每个页面。我在gae日志中找到了此追溯:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~gae/1.379703839015039430/main.py", line 68, in post
heap = get_times(tmp_url, 160)
File "/base/data/home/apps/s~gae/1.379703839015039430/main.py", line 106, in get_times
soup = BeautifulSoup(html)
File "libs/bs4/__init__.py", line 168, in __init__
self._feed()
File "libs/bs4/__init__.py", line 181, in _feed
self.builder.feed(self.markup)
File "libs/bs4/builder/_htmlparser.py", line 56, in feed
super(HTMLParserTreeBuilder, self).feed(markup)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/HTMLParser.py", line 114, in feed
self.goahead(0)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/HTMLParser.py", line 155, in goahead
startswith = rawdata.startswith
DeadlineExceededError
之所以失败,是因为您只有60秒钟的时间将响应返回给用户,而我猜这将花费更长的时间。
您将要使用此功能:https : //cloud.google.com/appengine/articles/deferred
创建一个有10分钟超时的任务。然后,您可以立即返回给用户,他们可以稍后通过另一个处理程序(您创建的)“提取”结果。如果收集所有URL花费的时间超过10分钟,则必须将其拆分为更多任务。
看到这个:https : //cloud.google.com/appengine/articles/deadlineexceedederrors
了解为什么您不能再走超过60秒的时间。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句