Example of using Python urlopen

Source: Internet
Author: User

1. Open a webpage to retrieve all the content

From Urllib Import Urlopen
Doc = Urlopen ( Http://www.baidu.com" ) . Read ()
Print Doc

2. Get the Http Header

From Urllib Import Urlopen
Doc = Urlopen ( Http://www.baidu.com" )
Print Doc . Info ()
Print Doc . Info () . Getheader ( 'Content-type' )

3. Use proxy

1. view environment variables

Print " "N " . Join ([ " % S = % S " % ( K , V ) For K , V In OS . Environ . Items ()])
Print OS . Getenv ( "Http_proxy" )

2. Set Environment Variables

Import OS 
OS . Putenv ( "Http_proxy" , "Http: // proxyaddr: <port>" )

3. Use a proxy

# Use http://www.someproxy.com: 3128 for http proxying
Proxies = { 'Http' : 'Http: // www.someproxy.com: 808080' }
Filehandle = Urllib . Urlopen ( Some_url , Proxies = Proxies )
# Don't use any proxies
Filehandle = Urllib . Urlopen ( Some_url , Proxies = {})
# Use proxies from environment-both versions are 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.