https://docs.python.org/2/howto/urllib2.html#data
http://zhuoqiang.me/python-urllib2-usage.html
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import os 5 import urllib 6 import urllib2 7 import re 8 import cookielib 9 10 httpHandler = urllib2.HTTPHandler(debuglevel=1) 11 httpsHandler = urllib2.HTTPSHandler(debuglevel=1) 12 opener = urllib2.build_opener(httpHandler, httpsHandler) 13 urllib2.install_opener(opener) 14 15 16 headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'} 17 values = { 18 #'username':'xxx', 19 #'password':'xxx', 20 } 21 postdata = urllib.urlencode(values) # 表单 22 23 # GET 24 req = urllib2.Request(url='https://www.baidu.com/', data=None, headers=headers) 25 response = urllib2.urlopen(req) 26 html = response.read() 27 response.close() 28 print html 29 30 # 图片等二进制数据 31 req = urllib2.Request(url='https://www.baidu.com/img/bd_logo1.png', data=postdata, headers=headers) 32 operate = opener.open(req) 33 data = operate.read() 34 operate.close() 35 36 f = open('baidu.png', 'wb') 37 f.write(data) 38 f.flush() 39 f.close()