import urllib.request
def demo():
"""
打开和读取URL网络资源
"""
f=urllib.request.urlopen('http://www.baidu.com')
str=f.read(2000).decode('utf-8')
print(str)
def getURLInfo(url,data,headers):
"""
创建Request对象
"""
req=urllib.request.Request(url,data,headers)
print('Full url : ',req.full_url)
print('Host : ',req.host)
print('Data : ',req.data)
if __name__ == '__main__':
url='http://www.baidu.com'
values={'wd':'python'}
data=urllib.parse.urlencode(values)
data=data.encode(encoding='UTF-8')
headers={'User-Agent':'Mozilla/4.0(compatoble;MSIE 5.5;Windows NT)'}
getURLInfo(url,data,headers)
python3 基于urllib模块的网络编程
最新推荐文章于 2021-02-19 23:12:55 发布