【问题标题】:Python urllib urlopen not workingPython urllib urlopen 不起作用
【发布时间】:2014-11-09 20:50:23
【问题描述】:

我只是想通过使用 urllib 模块从实时网络中获取数据,所以我写了一个简单的示例

这是我的代码:

import urllib

sock = urllib.request.urlopen("http://diveintopython.org/") 
htmlSource = sock.read()                            
sock.close()                                        
print (htmlSource)  

但我收到如下错误:

Traceback (most recent call last):
  File "D:\test.py", line 3, in <module>
    sock = urllib.request.urlopen("http://diveintopython.org/") 
AttributeError: 'module' object has no attribute 'request'

【问题讨论】:

    标签: python urllib


    【解决方案1】:

    您正在阅读错误的文档或错误的 Python 解释器版本。您尝试在 Python 2 中使用 Python 3 库。

    用途:

    import urllib2
    
    sock = urllib2.urlopen("http://diveintopython.org/") 
    htmlSource = sock.read()                            
    sock.close()                                        
    print htmlSource
    

    Python 2 urllib2 library 在 Python 3 中被 urllib.request 取代。

    【讨论】:

      【解决方案2】:

      这是我用来从 url 获取数据的方法,它很好,因为如果需要,您可以同时保存文件:

      import urllib
      
      result = urllib.urlretrieve("http://diveintopython.org/")
      
      print open(result[0]).read()
      

      输出:

      '<!DOCTYPE html><body style="padding:0; margin:0;"><iframe src="http://mcc.godaddy.com/park/pKMcpaMuM2WwoTq1LzRhLzI0" style="visibility: visible;height: 2000px;" allowtransparency="true" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" width="100%"></iframe></body></html>'
      

      编辑:urlretrieve 在 python 2 和 3 中工作

      【讨论】:

      • urlretrieve 可用于 Python 3 以及 legacy interface 的一部分。如果您想要的只是内存中的数据,那么urlretrieve 是错误的工具;您将数据下载到磁盘,然后从文件中再次打开它。
      • 此外,这甚至没有尝试回答所述问题。
      • 好吧,我说 urlretrieve 很好,因为您可以保存文件……这就是重点。如果有人告诉我他想从互联网上获取数据,为什么不提出类似的建议。
      • 我不知道它可以在 python 3 中工作 ;-) 虽然它已经过时了
      【解决方案3】:
      import requests
      import urllib
      
      link = "http://www.somesite.com/details.pl?urn=2344"
      
      f = urllib.request.urlopen(link)
      myfile = f.read()
      
      writeFileObj = open('output.xml', 'wb')
      writeFileObj.write(myfile)
      writeFileObj.close()
      

      【讨论】:

      • 通常一个好的答案不仅仅包含代码,还包含一些关于究竟修复了什么的信息。
      【解决方案4】:

      Python3 中,您可以使用 urlliburllib3

      urllib:

      import urllib.request
      with urllib.request.urlopen('http://docs.python.org') as response:
          htmlSource = response.read()
      

      urllib3:

      import urllib3
      http = urllib3.PoolManager()
      r = http.request('GET', 'http://docs.python.org')
      htmlSource = r.data
      

      更多细节可以在urllibpython 文档中找到。

      【讨论】:

        【解决方案5】:

        确保从urllib 导入requests,然后尝试这种格式,它对我有用:

        from urllib import request
        urllib.request.urlopen( )
        

        【讨论】:

          【解决方案6】:

          使用这个

              import cv2
              import  numpy as np
              import urllib //import urllib using pip
              import requests // import requests using pip`enter code here`
              url = "write your url"
              while True:
              imgresp = urllib.request.urlopen(url)
              imgnp = np.array(bytearray(imgresp.read()),dtype=np.uint8)
              img = cv2.imdecode(imgnp,-1)
              cv2.imshow("test",img)
              cv2.waitKey('q')
          

          【讨论】:

          • 如果您要发布答案,请尝试正确格式化您的代码 - 这不可读。此外,您似乎并没有真正解决这个问题。您正在使用问题中未提及的 numpy,然后似乎有一些注释掉的导入,最后它似乎是您正在下载的图像,这又不是问题所要求的。
          【解决方案7】:

          我刚刚问了同一个问题,现在已经超过 5 年了。

          请注意,给出的 URL 也是旧的,所以我替换了 python 欢迎页面

          我们可以在 python 3 中使用 requests 模块

          我用的是python 3,解决方法如下:

          import requests
          
          r = requests.get('https://www.python.org/')
          t = r.text
          
          print(t)
          

          这很有效并且很干净。

          【讨论】:

            【解决方案8】:

            对于python 3,正确的方法应该是:

            import cv2
            import numpy as np
            import urllib.request
            
            req = urllib.request.urlopen('http://answers.opencv.org/upfiles/logo_2.png')
            arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
            img = cv2.imdecode(arr, -1) # 'Load it as it is'
            
            cv2.imshow('image_name', img)
            if cv2.waitKey() & 0xff == 27: quit()
            

            这里你可以找到与urllib.request相关的文档

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-11-04
              • 2012-08-09
              • 2016-06-22
              • 1970-01-01
              • 2021-03-01
              • 2013-01-25
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多