【问题标题】:AttributeError: 'module' object has no attribute 'urlopen'AttributeError:“模块”对象没有属性“urlopen”
【发布时间】:2011-04-27 13:13:30
【问题描述】:

我正在尝试使用 Python 下载网站的 HTML 源代码,但收到此错误。

Traceback (most recent call last):  
    File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\DICParser\src\WebDownload.py", line 3, in <module>
     file = urllib.urlopen("http://www.python.org")
AttributeError: 'module' object has no attribute 'urlopen'

我在这里遵循指南:http://www.boddie.org.uk/python/HTML.html

import urllib

file = urllib.urlopen("http://www.python.org")
s = file.read()
f.close()

#I'm guessing this would output the html source code?
print(s)

我正在使用 Python 3。

【问题讨论】:

    标签: python python-3.x urllib


    【解决方案1】:

    这适用于 Python 2.x。

    对于 Python 3,请查看 docs

    import urllib.request
    
    with urllib.request.urlopen("http://www.python.org") as url:
        s = url.read()
        # I'm guessing this would output the html source code ?
        print(s)
    

    【讨论】:

    • 嗨 Eumiro,在 Python 中使用 'with' 语句我猜它会在使用完成后自动关闭连接?类似于 C# 中的 use 语句?
    • @Sergio:完全正确!通过缩进,您可以看到您的文件仍然打开的位置。
    • 您好@eumiro,当我输入s = url.read() 时出现“IndentationError:预期缩进块”错误,请问我该如何解决? x
    • @KarenChan 你在s=url.read() 之前缺少一个缩进;前面有 4 个空格吗?
    • 我用你的方法得到HTTPError: HTTP Error 503: Service Unavailable
    【解决方案2】:
    import urllib.request as ur
    s = ur.urlopen("http://www.google.com")
    sl = s.read()
    print(sl)
    

    在 Python v3 中,“urllib.request”本身就是一个模块,因此这里不能使用“urllib”。

    【讨论】:

      【解决方案3】:
      import urllib.request as ur
      
      filehandler = ur.urlopen ('http://www.google.com')
      for line in filehandler:
          print(line.strip())
      

      【讨论】:

        【解决方案4】:

        兼容 Python 2+3 的解决方案是:

        import sys
        
        if sys.version_info[0] == 3:
            from urllib.request import urlopen
        else:
            # Not Python 3 - today, it is most likely to be Python 2
            # But note that this might need an update when Python 4
            # might be around one day
            from urllib import urlopen
        
        
        # Your code where you can use urlopen
        with urlopen("http://www.python.org") as url:
            s = url.read()
        
        print(s)
        

        【讨论】:

        • with urlopen("http://www.python.org") as url: 在带有AttributeError: addinfourl instance has no attribute '__exit__' 的python2 中不起作用。需要写url = urlopen("http://www.python.org")
        【解决方案5】:

        要让 'dataX = urllib.urlopen(url).read()' 在 python3 中工作(这将对 python2) 来说是正确的,你必须只改变 2 个小东西。

        1: urllib 语句本身(中间加上.request):

        dataX = urllib.request.urlopen(url).read()
        

        2:前面的import语句(从'import urllib'改为:

        import urllib.request
        

        它应该在 python3 中工作:)

        【讨论】:

          【解决方案6】:

          对于 python 3,试试这样的:

          import urllib.request
          urllib.request.urlretrieve('http://crcv.ucf.edu/THUMOS14/UCF101/UCF101/v_YoYo_g19_c02.avi', "video_name.avi")
          

          它将视频下载到当前工作目录

          I got help from HERE

          【讨论】:

            【解决方案7】:

            python3的解决方案:

            from urllib.request import urlopen
            
            url = 'http://www.python.org'
            file = urlopen(url)
            html = file.read()
            print(html)
            

            【讨论】:

            • 简单易懂,适合初学者。谢谢
            【解决方案8】:

            如果您的代码使用 Python 2.x 版,您可以执行以下操作:

            from urllib.request import urlopen
            urlopen(url)
            

            顺便推荐另一个模块requests,使用起来更友好。可以使用pip安装,这样使用:

            import requests
            requests.get(url)
            requests.post(url)
            

            【讨论】:

              【解决方案9】:
              import urllib
              import urllib.request
              from bs4 import BeautifulSoup
              
              
              with urllib.request.urlopen("http://www.newegg.com/") as url:
                  s = url.read()
                  print(s)
              soup = BeautifulSoup(s, "html.parser")
              all_tag_a = soup.find_all("a", limit=10)
              
              for links in all_tag_a:
                  #print(links.get('href'))
                  print(links)
              

              【讨论】:

                【解决方案10】:

                一种可能的方法:

                import urllib
                ...
                
                try:
                    # Python 2
                    from urllib2 import urlopen
                except ImportError:
                    # Python 3
                    from urllib.request import urlopen
                

                【讨论】:

                  【解决方案11】:

                  更改两行:

                  import urllib.request #line1
                  
                  #Replace
                  urllib.urlopen("http://www.python.org")
                  #To
                  urllib.request.urlopen("http://www.python.org") #line2
                  

                  如果您收到 ERROR 403: Forbidden Error 异常,请尝试以下操作:

                  siteurl = "http://www.python.org"
                  
                  req = urllib.request.Request(siteurl, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36'})
                  pageHTML = urllib.request.urlopen(req).read()
                  

                  希望您的问题得到解决。

                  【讨论】:

                    【解决方案12】:

                    使用第三方sixmodule让你的代码兼容Python2和Python3。

                    from six.moves import urllib
                    urllib.request.urlopen("<your-url>")
                    

                    【讨论】:

                    • 这样可以导入六个模块 from Six.moves import urllib
                    【解决方案13】:
                    imgResp = urllib3.request.RequestMethods.urlopen(url)
                    

                    在使用 urlopen 之前添加这个 RequestMethods

                    【讨论】:

                      猜你喜欢
                      • 2017-11-22
                      • 1970-01-01
                      • 2010-11-18
                      相关资源
                      最近更新 更多