python3.x above crawler use problem Urllib (cannot use URLLIB2)

Source: Internet
Author: User

Question one: Python 3.x or above includes URLLIB2, integrating URLLIB2 and Urllib.

and introduce the module into one, only import urllib

# import urllib# Import urllb2# Correct use of import urllib

  

  So you cannot use the Urllib2.urlopen () method, or you will get an error without this module.

Nameerror:name ' URLLIB2 ' is not defined

  

3.x or above version # Urllib2.open not available, use Urllib.request module instead of URLLIB2

1 Import urllib.request 2 3 response = Urllib.request.urlopen (' http//www.baidu.com')

Problem Two: variable name cannot be the same as introducing module name method, otherwise it will cause an error

  

1 Importurllib.request2 3 #wrong wording4 5Request = Urllib.request.urlopen ('/HTTPwww.baidu.com')6 7 #otherwise TypeError: ' Module ' object is not callable8 9 #correct wordingTen  OneRQS = Urllib.request.urlopen ('/HTTPwww.baidu.com')

Issue four print output issues

  The 3.x version of print has become a method of outputting anything that needs to be included in () brackets, or there will be a problem

The first step of the initial approach to crawl Baidu home

1 Import urllib.request 2 3 RQS = Urllib.request.urlopen ('http://www.baidu.com')4 5 html = rqs.read ()67print(HTML)

python3.x above crawler use problem Urllib (cannot use URLLIB2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.