Python Urlopen () function sample sharing

Source: Internet
Author: User
All right, let's just take a look at a few examples.

First, open a Web page to get all the content

Copy the Code code as follows:


From Urllib import Urlopen
doc = Urlopen ("http://www.baidu.com"). Read ()
Print doc

Second, get the HTTP header

Copy the Code code as follows:


From Urllib import Urlopen
doc = Urlopen ("http://www.baidu.com")
Print Doc.info ()
Print Doc.info (). GetHeader (' Content-type ')

Iii. Use of agents

1. View Environment variables

Copy the Code code as follows:


Print "n". Join (["%s=%s"% (k, v) for K, V in Os.environ.items ()])
Print os.getenv ("Http_proxy")

2. Setting Environment variables

Copy the Code code as follows:


Import OS
Os.putenv ("Http_proxy", "HTTP://PROXYADDR: ")

3. Using proxies

Copy the Code code as follows:


# Use http://www.someproxy.com:3128 for HTTP proxying
Proxies = {' http ': ' http://www.someproxy.com:3128 '}
FileHandle = Urllib.urlopen (Some_url, proxies=proxies)
# Don ' t use any proxies
FileHandle = Urllib.urlopen (Some_url, proxies={})
# Use proxies from Environment-both versions is equivalent
FileHandle = Urllib.urlopen (Some_url, Proxies=none)
FileHandle = Urllib.urlopen (Some_url)
  • 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.