资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Python爬虫urllib模块:get方式

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

创新互联建站是一家专业提供榕江企业网站建设,专注与网站制作、成都做网站、H5场景定制、小程序制作等业务。10年已为榕江众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

格式:

  导入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的程序如下:

import urllib.request

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

运行结果:

Python 爬虫 urllib模块:get方式

2.decode的程序如下:

import urllib.request

response = urllib.request.urlopen('www.baidu.com')
html = response.read()

print(html.decode())
print("------------------------------------------------------------------")
print("------------------------------------------------------------------")
print(response.status)

运行结果:







    
    
    

    
    
    
    










    
    百度一下,你就知道
    

html,body{height:100%}
.
.
.
.










------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
200

当前名称:Python爬虫urllib模块:get方式
当前地址:http://cdkjz.cn/article/josjhj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220