繁体   English   中英

尝试进行API调用时,为什么我的python脚本停止执行?

[英]Why does my python script stop executing when I try to make my API call?

我正在尝试运行此脚本,仅打印出通过调用AlchemyAPi获得的JSON对象的某些内容,但是由于某些原因,我的程序在接受了用户的URL后停顿了。 我使用Ctrl-C强制关闭它,这是我得到的错误:

Traceback (most recent call last):
File "test2.py", line 18, in <module>
json = urllib2.urlopen(url).read()
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "/usr/lib/python2.7/httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 992, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 776, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 757, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 562, in create_connection
sock.connect(sa)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)

这是我要运行的脚本:

from alchemyapi import AlchemyAPI
from encodings import hex_codec
from encodings import ascii
import httplib,urllib2
import json
import sys

alchemyapi = AlchemyAPI()

prompt = "> "
api_key = "something"
url = raw_input(prompt)

try:
    #Accessing Alchemy API to get the relevance
    url = "http://access.alchemyapi.com/calls/url/URLGetRankedKeywords?apikey=%s&url=%s&outputMode=json" % (api_key,url)

    # sample url: http://access.alchemyapi.com/calls/url/URLGetRankedKeywords?apikey=83da2d0deaef076648b28972905d2f8c1f5e3c0f&url=http://www.google.com&outputMode=json 
    json = urllib2.urlopen(url).read()

    #Parsing the json payload MUST TO THIS TO CONVERT INTO JSON OBJECT 
    the_data = eval(json)

    #Retrieving relevance from the payload. SELECT THE DATA THAT YOU NEED USING THE TREE
    print the_data ['keywords'][0]['relevance']

    print the_data ['language']



except urllib2.URLError, e:
    if hasattr(e, 'reason'):
    print 'We failed to reach a server.\r'
    print 'Reason: ', e.reason
    elif hasattr(e, 'code'):
    print 'Sorry! Looks like Klout doesn\'t have info on that soldier!\r'
    print 'Error code: ', e.code

我很确定错误在显示json = urllib2.urlopen(url).read() ,但是我不知道为什么会这样。 我该如何解决这个问题?

用您的钥匙,这对您有什么作用?

#!/usr/bin/python

import json as json_mod
import pprint
import urllib2

api_key = 1 # or whatever...
url = 'http://stromberg.dnsalias.org/~strombrg/tech-tidbits.html'

# Accessing Alchemy API to get the relevance
url = "http://access.alchemyapi.com/calls/url/URLGetRankedKeywords?apikey=%s&url=%s&outputMode=json" % (api_key,url)

# sample url: http://access.alchemyapi.com/calls/url/URLGetRankedKeywords?apikey=83da2d0deaef076648b28972905d2f8c1f5e3c0f&url=http://www.google.com&outputMode=json
text = urllib2.urlopen(url).read()

json = json_mod.loads(text)

pprint.pprint(json)

对我来说,它会打印一个描述身份验证错误的JSON对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM