python Urllib2模块

# -*-coding:utf-8-*-
__author__ = 'magicpwn'
import urllib2
import urllib
import Cookie
# 无参数时,一般访问用法及返回信息的获取
respon = urllib2.urlopen('http://www.baidu.com')
print 'code:', respon.getcode(), 'URL:', respon.geturl()
print '==================Headers================='
print respon.info()             # 头域字典
print respon.info()['date']     # 头域信息的获取
print '==================Data===================='
print respon.read()

query_args = {'name': 'admin', 'password': '123456'}
encoded_args = urllib.urlencode(query_args)
# 设置get参数,使用http get方式,传递参数放在URL里
urlfinal = 'http://www.baidu.com/?' + encoded_args
respon2 = urllib2.urlopen(urlfinal)
print respon2.getcode()
# 使用post方式,传递参数放在数据包里
urlfinal2 = 'http://www.baidu.com/'
respon3 = urllib2.urlopen(urlfinal2, encoded_args)
print respon3.getcode()

# 增加/修改发出首部     # 增加Cookie首部可以使用这个方法
request = urllib2.Request('http://magicpwn.blog.51cto.com/')
request.add_header('User-agent', 'just test')
request.add_header('Cookie', 'www51cto=6CD8axxx0D9ExxxF75xxD7xxxx7E77hIxO; pub_cookietime=2xx2xx87;')
respon4 = urllib2.urlopen(request)
print respon4.info()
print respon4.read()

# Request 可以增加首部也可以增加Data,增加data后,http方法自动有get改为Post
request.add_data(encoded_args)
respon5 = urllib2.urlopen(request)
print respon5.getcode()
# Cookie一般由服务器创建,其使用Cookie模块进行创建和设置Cookie
# co = Cookie.simpleCookie()
# co['mycookie'] = 'cookie_value'
# 此时print co
# > Set-Cookie: mycookie=cookie_value
C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/te5t/testurllib2.py
code: 200 URL: http://www.baidu.com
==================Headers=================
Date: Sun, 16 Aug 2015 15:58:48 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: Close
Vary: Accept-Encoding
Set-Cookie: BAIDUID=436BFDFEE5BA933A28CEB2C2ACED5773:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=436BFDFEE5BA933A28CEB2C2ACED5773; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1439740728; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=14346_1444_16780_14430_12867_16520_16800_16660_16427_16514_15261_12027_13932_16720_16867; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Cache-Control: private
Cxy_all: baidu+39e7b20e6ee9474fa191e989f65b8e05
Expires: Sun, 16 Aug 2015 15:58:07 GMT
X-Powered-By: HPHP
Server: BWS/1.1
X-UA-Compatible: IE=Edge,chrome=1
BDPAGETYPE: 1
BDQID: 0xd89ef62700000011
BDUSERID: 0

Sun, 16 Aug 2015 15:58:48 GMT
==================Data====================
<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><title>百度一下,你就知道</title><div id="u"><a class="toindex" href="/">百度首页</a><a href="javascript:;" name="tj_settingicon" class="pf">设置<i class="c-icon c-icon-triangle-down"></i></a><a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" class="lb" onclick="return false;">登录</a></div><div id="u1"><a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a></html>(html代码有删除)

200
200
Server: Tengine
Date: Sun, 16 Aug 2015 15:58:48 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Set-Cookie: PHPSESSID=438c259ccd7d669272c849abec0d3c3e; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: lastvisit=0%091439740728%09%2Findex.php%3F; expires=Mon, 15-Aug-2016 15:58:48 GMT; path=/; domain=.blog.51cto.com
If-Modified-Since: Sat, 15 Aug 2015 16:00:00 GMT
Load-Balancing: web39

200

进程已结束,退出代码0

spacer.gif

wKioL1XQu2ng9siaAAKkIN-iWaM188.jpg

wKiom1XQuWHQZIqrAAQj43fBCYo262.jpg

wKioL1XQu2qjEdolAAOjHsJuW2M132.jpg

wKiom1XQuWLy6QCdAAPtoGOhNZ0756.jpg


你可能感兴趣的:(python,urllib2)