lynda에서이 데모 프로그램을 Python 3을 사용하여 테스트하려고합니다. Pycharm을 IDE로 사용하고 있습니다. 이미 요청 패키지를 추가하고 설치했지만 프로그램을 실행하면 정상적으로 실행되고 "프로세스가 종료 코드 0으로 완료되었습니다."라는 메시지가 표시되지만 print 문의 출력이 표시되지 않습니다. 내가 어디로 잘못 가고 있니?모듈 urllib.request가 데이터를 가져 오지 않습니다
import urllib.request # instead of urllib2 like in Python 2.7
import json
def printResults(data):
# Use the json module to load the string data into a dictionary
theJSON = json.loads(data)
# now we can access the contents of the JSON like any other Python object
if "title" in theJSON["metadata"]:
print(theJSON["metadata"]["title"])
# output the number of events, plus the magnitude and each event name
count = theJSON["metadata"]["count"];
print(str(count) + " events recorded")
# for each event, print the place where it occurred
for i in theJSON["features"]:
print(i["properties"]["place"])
# print the events that only have a magnitude greater than 4
for i in theJSON["features"]:
if i["properties"]["mag"] >= 4.0:
print("%2.1f" % i["properties"]["mag"], i["properties"]["place"])
# print only the events where at least 1 person reported feeling something
print("Events that were felt:")
for i in theJSON["features"]:
feltReports = i["properties"]["felt"]
if feltReports != None:
if feltReports > 0:
print("%2.1f" % i["properties"]["mag"], i["properties"]["place"], " reported " + str(feltReports) + " times")
# Open the URL and read the data
urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
webUrl = urllib.request.urlopen(urlData)
print(webUrl.getcode())
if webUrl.getcode() == 200:
data = webUrl.read()
data = data.decode("utf-8") # in Python 3.x we need to explicitly decode the response to a string
# print out our customized results
printResults(data)
else:
print("Received an error from server, cannot retrieve results " + str(webUrl.getcode()))
오히려 나는 코드를 붙여 넣기를 잊어 버렸다. 모듈을 설치 한 후 IDE를 다시 시작하지 않았습니다. 지금 막 "관리자 권한으로 실행"을 통해 깨닫고 시도했습니다. 이상하게도 지금 일하는 것 같습니다. – dexter87