2013-06-26 6 views
1

ok 이것이 내 마지막 질문입니다. 그래서 마침내 좋은 인쇄물을 발견하고 작동하지만 내 문제는 누군가 나를 위해 이것을보고 무엇이 잘못 됐는지를 알면 오류가 발생합니다. 그 큰NameError : name 'urlopen'이 정의되지 않았습니다.

import urllib 
import json 

request = urlopen("http://api.exmaple.com/stuff?client_id=someid&client_secret=randomsecret") 
response = request.read() 
json = json.loads(response) 
if json['success']: 
    ob = json['response']['ob'] 
    print ("The current weather in Seattle is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF']) 
else: 
    print ("An error occurred: %s") % (json['error']['description']) 
request.close() 

것 여기에 오류

Traceback (most recent call last): 
File "thing.py", line 4, in <module> 
request = urlopen("http://api.exmaple.com/stuff?client_id=someid&client_secret=randomsecret") 
NameError: name 'urlopen' is not defined 

답변

9

당신은 이름 urlopen을 가져 오지 않았다이다. 당신은 python3를 사용하고 있기 때문에

, 당신이 urllib.request가 필요합니다 명시 적으로

from urllib.request import urlopen 
req = urlopen(...) 

또는를 request 모듈을 참조 python2에

import urllib.request 
req = request.urlopen(...) 

이 될 것이라고

from urllib import urlopen 

또는 urllib.urlopen을 사용하십시오.


참고 : 또한 좋은 생각이 아니다 이름 json을 무시하고 있습니다.

+0

지금이 오류 역 추적 (마지막으로 가장 최근에 전화를) 받고 이름 urlopen을 가져올 수 없습니다 – apples723

+0

@ apples723 python3을 사용하고 있습니다. 업데이트 된 답변보기 – Elazar

+0

내가 말한 내용이 바뀌었지만이 오류가 발생했습니다. Traceback (가장 최근에 마지막으로 호출) : 파일 "C : /Users/Grant/Desktop/finaly.py", 줄 4, 요청 = urllib.urlopen : //api.aerisapi.com/observations/Urbandale,IA CLIENT_ID = QD2ToJ2o7MKAX47vrBcsC & client_secret = 0968kxX4DWybMkA9GksQREwBlBlC4njZw9jQNqdO ") 나가서 설명하자면 NameError : 이름 'URLLIB은'내 파이에 코딩이 학습에 hasle하지만 toatly 새로운 메신저 미안 정의되어 있지 않은 것을 내가 got week ago – apples723

2

파이썬은 urlopen (라인 4)를 참조 할 때 urlopenurllib 인 것을 모릅니다. 당신은 두 가지 옵션이 있습니다

  1. 가되어 파이썬에게 - urlopen에 대한 모든 참조는 urllib에서 한 것을 파이썬에게 urllib.urlopen
  2. urlopen 교체 : from urllib import urlopen
+0

첫 번째로이 오류가 발생 함 Traceback (가장 최근에 마지막으로 호출) : 파일 "C : /Users/Grant/Desktop/finally2.py", 줄 4, request = urllib.urlopen ("http : // api.aerisapi.com/observations/Urbandale,IA?client_id=QD2ToJ2o7MKAX47vrBcsC&client_secret=0968kxX4DWybMkA9GksQREwBlBlC4njZw9jQNqdO " 파일) AttributeError은 : '모듈'개체가 어떤 속성 'urlopen'를 갖지 않고 1 초 동안 내가 마지막 (가장 최근 통화) thisTraceback를 얻을" C : /Users/Grant/Desktop/finally2.py C ", 라인 1, 에서 URLLIB 가져 오기에서이 ImportError를 urlopen : 파일 : – apples723

1

을에 선 import urllib 교체 다음과 같아야합니다.

import urllib 
import json 

request = urllib.urlopen("http://api.example.com/endpoint?client_id=id&client_secret=secret") 
response = request.read() 
json = json.loads(response) 

if json['success']: 
    ob = json['response']['ob'] 
    print ("The current weather in Seattle is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF']) 

else: 
    print ("An error occurred: %s") % (json['error']['description']) 

request.close() 

urllib 라이브러리에서 urlopen() 메소드를 호출하십시오. ImportError를 urlopen URLLIB 가져 오기에서 에서, "/Users/Grant/Desktop/finaly.py C", 라인 1 : 파일 :

+0

urlopen 이름을 가져올 수 없습니다 난이 오류 역 추적 (마지막으로 가장 최근 통화)를 얻을":/Users/Grant/Desktop/finally2.py ", 줄 1, urllib에서 가져 오기 urlopen ImportError : 내 추천 단어가 아닌 urlopen – apples723

+0

을 가져올 수 없습니다. 위의 내용을 정확히 실행 해보십시오. 나는 그것을 테스트하고 작동합니다. – msturdy

+0

r 실행중인 python 2 또는 3 im 실행 중 3 – apples723

관련 문제