本程序以爬取 百度 首页为例

格式:

导入urllib.request

打开爬取的网页: response = urllib.request.urlopen('网址')

读取网页代码: html = response.read()

打印:

1.不decode

print(html) #爬取的网页代码会不分行,没有空格显示,很难看

2.decode

print(html.decode()) #爬取的网页代码会分行,像写规范的代码一样,看起来很舒服

查询请求结果:

a. response.status # 返回 200:请求成功 404:网页找不到,请求失败

b. response.getcode() # 返回 200:请求成功 404:网页找不到,请求失败


1.不decode的程序如下:

importurllib.requestresponse=urllib.request.urlopen('www.baidu.com')html=response.read()print(html)print("------------------------------------------------------------------")print("------------------------------------------------------------------")print(response.status)


运行结果:




2.decode的程序如下:

importurllib.requestresponse=urllib.request.urlopen('www.baidu.com')html=response.read()print(html.decode())print("------------------------------------------------------------------")print("------------------------------------------------------------------")print(response.status)


运行结果:

<!DOCTYPEhtml><!--STATUSOK--><html><head><metahttp-equiv="content-type"content="text/html;charset=utf-8"><metahttp-equiv="X-UA-Compatible"content="IE=Edge"><metacontent="always"name="referrer"><metaname="theme-color"content="#2932e1"><linkrel="shortcuticon"href="/favicon.ico"type="image/x-icon"/><linkrel="search"type="application/opensearchdescription+xml"href="/content-search.xml"title="百度搜索"/><linkrel="icon"sizes="any"maskhref="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg"><linkrel="dns-prefetch"href="//s1.bdstatic.com"/><linkrel="dns-prefetch"href="//t1.baidu.com"/><linkrel="dns-prefetch"href="//t2.baidu.com"/><linkrel="dns-prefetch"href="//t3.baidu.com"/><linkrel="dns-prefetch"href="//t10.baidu.com"/><linkrel="dns-prefetch"href="//t11.baidu.com"/><linkrel="dns-prefetch"href="//t12.baidu.com"/><linkrel="dns-prefetch"href="//b1.bdstatic.com"/><title>百度一下,你就知道</title><styleid="css_index"index="index"type="text/css">html,body{height:100%}....</body></html>------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------200