Python urllib urlopen not working

后端 未结 8 1186
甜味超标
甜味超标 2021-01-04 07:29

I am just trying to fetch data from a live web by using the urllib module, so I wrote a simple example

Here is my code:

import urllib

sock = urllib         


        
8条回答
  •  长情又很酷
    2021-01-04 07:56

    You are reading the wrong documentation or the wrong Python interpreter version. You tried to use the Python 3 library in Python 2.

    Use:

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

    The Python 2 urllib2 library was replaced by urllib.request in Python 3.

提交回复
热议问题