現在、Reddit のフロント ページからトップ ヘッドラインをダウンロードするためのスクリプトがあり、ほぼ常に機能します。時折、以下の例外が発生します。try
コードを保護するためにandステートメントを挿入する必要があることはわかってexcept
いますが、どこに配置すればよいでしょうか?
クロール:
def crawlReddit():
r = praw.Reddit(user_agent='challenge') # PRAW object
topHeadlines = [] # List of headlines
for item in r.get_front_page():
topHeadlines.append(item) # Add headlines to list
return topHeadlines[0].title # Return top headline
def main():
headline = crawlReddit() # Pull top headline
if __name__ == "__main__":
main()
エラー:
Traceback (most recent call last):
File "makecall.py", line 57, in <module>
main() # Run
File "makecall.py", line 53, in main
headline = crawlReddit() # Pull top headline
File "makecall.py", line 34, in crawlReddit
for item in r.get_front_page():
File "/Users/myusername/Documents/dir/lib/python2.7/site-packages/praw/__init__.py", line 480, in get_content
page_data = self.request_json(url, params=params)
File "/Users/myusername/Documents/dir/lib/python2.7/site-packages/praw/decorators.py", line 161, in wrapped
return_value = function(reddit_session, *args, **kwargs)
File "/Users/myusername/Documents/dir/lib/python2.7/site-packages/praw/__init__.py", line 519, in request_json
response = self._request(url, params, data)
File "/Users/myusername/Documents/dir/lib/python2.7/site-packages/praw/__init__.py", line 383, in _request
_raise_response_exceptions(response)
File "/Users/myusername/Documents/dir/lib/python2.7/site-packages/praw/internal.py", line 172, in _raise_response_exceptions
response.raise_for_status()
File "/Users/myusername/Documents/dir/lib/python2.7/site-packages/requests/models.py", line 831, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 503 Server Error: Service Unavailable