【问题标题】:Module urllib.request not getting data模块 urllib.request 未获取数据
【发布时间】:2016-11-28 21:01:34
【问题描述】:

我正在尝试使用 Python 3 从 lynda 测试这个演示程序。我使用 Pycharm 作为我的 IDE。我已经添加并安装了请求包,但是当我运行程序时,它运行干净并显示消息“进程完成,退出代码 0”,但不显示打印语句的任何输出。我哪里错了?

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()))

【问题讨论】:

    标签: python pycharm urllib2 urllib python-3.5


    【解决方案1】:

    不确定您是否有意忽略了这一点,但该脚本实际上并没有执行超出导入和函数定义的任何代码。假设您没有故意将其遗漏,您需要在文件末尾添加以下内容。

    if __name__ == '__main__':
        data = "" # your data
        printResults(data)
    

    检查__name__ 等于"__main__" 只是为了让您的代码仅在文件显式运行时才执行。要在访问文件时始终运行 printResults(data) 函数(例如,如果将其导入另一个模块),您可以像这样在文件底部调用它:

    data = "" # your data
    printResults(data)
    

    【讨论】:

    • 哦,不,我有 main... 只是忘记将其粘贴到代码中。安装模块后我没有重新启动 IDE。我现在才意识到并尝试使用“以管理员身份运行”。奇怪的是现在似乎可以工作了。
    【解决方案2】:

    安装模块后我必须重新启动 IDE。我现在才意识到并尝试使用“以管理员身份运行”。奇怪的是现在似乎可以工作。但不确定这是否是临时错误,因为即使没有重新启动,它也能够检测到模块及其方法。

    【讨论】:

      【解决方案3】:

      您的 cmets re:必须重新启动您的 IDE 让我认为 pycharm 可能不会自动检测新安装的 python 包。这个 SO 答案似乎提供了一个解决方案。

      SO answer

      【讨论】:

      • 我完全通过上述过程安装了新包。通过进入设置,然后进入解释器。另外我认为pycharm确实检测到它的原因是因为模块名称下没有红线,并且我在编写代码时也能够调用模块中包含的函数。这让我认为这是一个临时错误。由于红线不存在并且模块可用,我没想过重新启动它。重启成功了,所以有点困惑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 2019-01-28
      • 2015-05-17
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      相关资源
      最近更新 更多