Home  >  Article  >  Backend Development  >  What should I do if I want to use the urllib2 package in python3.6?

What should I do if I want to use the urllib2 package in python3.6?

(*-*)浩
(*-*)浩Original
2019-07-01 14:18:024354browse

Python3.6.6 or python3.x cannot find the urllib2 syntax problem. After correcting it, an error will be reported that the urllib2 package is not installed.

What should I do if I want to use the urllib2 package in python3.6?

Through pip install urllib2 will also prompt that the package cannot be found. (Recommended learning: Python video tutorial)

Through pip3 install urllib2 will also prompt that the package cannot be found.

This is because builtwith depends on the urllib2 package. However, the urllib2 toolkit in Pyhton2 was split into two packages: urllib.request and urllib.error in Python3. As a result, the package cannot be found and there is no way to install it.

So you need to install the two packages urllib.request and install urllib.error, and then modify the import urllib2 in the builtwith package to import urllib.request and import urllib.error.

At the same time, the method functions in the code also need to be modified. Basically, urllib2.xxx is modified to urllib.request.xxx.

An example is provided below to help everyone understand:

#下载网页
import urllib.request
import urllib.error

def download(url):
    print('Downloading:',url)
    try:
        html=urllib.request.urlopen(url).read()
    except urllib.error as e:
        print('download error:',e.reason)
        html=None
    return html
download('http://example.webscraping.com/')

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of What should I do if I want to use the urllib2 package in python3.6?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn