Examples of common commands for accessing and capturing web pages using python

Source: Internet
Author: User
Tags line web
This article mainly introduces information about common commands for accessing and capturing web pages using python. For more information about common commands for accessing and capturing web pages using python, see the following article, for more information, see

Common commands for accessing and capturing web pages using python

Simple webpage capturing:

Import urllib. request url = "http://google.cn/" response = urllib. request. urlopen (url) # Return file object page = response. read ()

Directly save the URL as a local file:

Import urllib. request url = "http://google.cn/" response = urllib. request. urlopen (url) # Return file object page = response. read ()

POST method:

Import urllib. parse import urllib. request url = "http://liuxin-blog.appspot.com/messageboard/add" values = {"content": "command line web page request test"} data = urllib. parse. urlencode (values) # Create a request object req = urllib. request. request (url, data) # obtain the data returned by the server response = urllib. request. urlopen (req) # process data page = response. read ()

GET method:

Import urllib. parse import urllib. request url = "http://www.google.cn/webhp" values = {"rls": "ig"} data = urllib. parse. urlencode (values) theurl = url + "? "+ Data # Create a request object req = urllib. request. request (theurl) # get the data returned by the server response = urllib. request. urlopen (req) # process data page = response. read ()

There are 2 common methods, geturl (), info ()

Geturl () is set to identify whether there is server-side URL redirection, while info () contains a series of information.

To solve Chinese problems, encode () encoding dencode () decoding is used:

The above is a detailed description of the examples of common commands for accessing and capturing web pages in python. For more information, see other related articles in the first PHP community!

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.