import urllib.request
response1 = urllib.request.urlopen("http://baike.baidu.com/wikicategory/view?categoryName=恐龙大全")
html = response1.read() #读出来
html = html.decode("utf-8") #使用网页编码格式
print(html)
urlopen() 函数。
urllib.request.urlopen(
url,data=None,[timeout, ]*,cafile=None,capath=None,cadefault=False,context=None)
**url 参数:**URL的字符串、一个urllib.request对象
**data参数:**data用来指明发往服务器请求中的额外的信息,
1.data必须是一个字节数据对象(Python的bytes object)
2.data必须符合标准the standard application/x-www-form-urlencoded format,怎么得到这种标准结构的data呢?使用urllib.parse.urlencode()将自定义的data转换成标准格式,而这个函数所能接收的参数类型是pyhon中的mapping object(键/值对,如dict) or a sequence of two-element tuples(元素是tuple的列表)。
3.data也可以是一个可迭代的对象,这种情况下就需要配置response对象中的Conten-length,指明data的大小。
4.data默认是None,此时以GET方式发送请求;当用户给出data参数的时候,改为POST方式发送请求。