get请求,直接在url请求后面添加数据:
import urllib.request
url="http:// www.baidu.com/s? wd="
key="韦玮老师"

key_code=urllib.request.quote(key)
url_all=url+key_code
req=urllib.request.Request(url_all)
data=urllib.request.urlopen(req).read()
fh=open("D:/Python35/myweb/part4/5.html", "wb")
fh.write(data)
fh.close()
post请求在创建Request对象,参数包括url和*要传递的数据*postdata
import urllib.request

import urllib.parse

url = "http://www.iqianyue.com/mypost/"
postdata =urllib.parse.urlencode({
"name":"ceo@iqianyue.com",
"pass":"aA123456"
}).encode('utf-8') #将数据使用urlencode编码处理后,使用encode()设置为utf-8编码

req = urllib.request.Request(url, postdata)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0')
data=urllib.request.urlopen(req).read()
fhandle=open("D:/Python35/myweb/part4/6.html", "wb")
fhandle.write(data)
fhandle.close()