python3 基于urllib模块的网络编程

来源:互联网 发布:中医理疗器材淘宝网 编辑:程序博客网 时间:2024/06/05 17:15
import urllib.requestdef 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)

0 0
原创粉丝点击