Writing a web crawler in Python (ii): Using URLLIB2 to crawl Web content through specified URLs

Source: Internet
Author: User
Tags http request in python

The so-called web crawl, is the URL address specified in the network resources from the network stream to read out, save to the local.

Similar to using the program to simulate the function of IE browser, the URL is sent as the content of the HTTP request to the server side, and then read the server-side response resources.

In Python, we use the URLLIB2 component to crawl a Web page.

URLLIB2 is a Python component that gets the URLs (uniform Resource Locators).

It provides a very simple interface in the form of a urlopen function.

The simplest URLLIB2 application code requires only four lines.

We create a new file urllib2_test01.py to feel the role of URLLIB2:

Import urllib2  
response = Urllib2.urlopen (' http://www.baidu.com/')  
html = response.read ()  
Print HTML

Press F5 to see the results of the run:

Related Article

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.