【发布时间】:2014-10-28 13:56:41
【问题描述】:
在我的 RequestHandler 子类中,我正在尝试获取 url 范围:
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 秒后重试。
有没有办法获取大量的网址?可能是更优化的还是smth? 多达数百页?
更新:
我正在使用 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
【问题讨论】:
-
所有请求是否都发送到同一个服务器/域名?
-
@jDourlens 是的。
-
您的所有请求是否在 60 秒内完成?您只有 60 秒的时间来返回请求。试着把它放到一个任务或类似的任务中。
-
服务器可能会在短时间内阻止来自同一客户端的多次访问他的页面。也许是为了 ddos 或报废保护.. 与不同的客户端重新连接或在通话之间等待(睡眠),您无能为力
-
查看日志并提供有关错误的更多信息。这可能只是来自应用引擎的截止日期错误
标签: python google-app-engine python-2.7 webapp2