2019-05-04


title: python3爬虫
date: 2019-03-03 09:15:29
tags:


requests库的使用

我们使用python3编写爬虫可以使用requests和urllib库,这两个都是python的第三方库,不过requests库更加的简单,因此我选用requests来进行爬虫的学习,在学习之前我们需要先配置自己的环境。

  • 环境的搭建

    1. 安装python3,直接在python官网上下载即可
    2. 进入dos窗口输入python,检测python是否安装成功
    3. 安装requests库,使用python的包管理工具pip,在dos窗口里运行命令pip install requests
    4. 检测requests是否安装成功,在python的交互模式里输入import requests,如果没报错表示成功
  • 第一个网络爬虫

    1. import requests
      url="https://baidu.com"
      data=requests.get(url)
      print(data.text)
      
    2. 返回的结果
      
      
      <!DOCTYPE html>
      
      <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>ç�¾åº¦ä¸�ä¸�ï¼�ä½ å°±ç�¥é��</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=ç�¾åº¦ä¸�ä¸� class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>æ�°é�»</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>å�°å�¾</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>è§�é¢�</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>è´´å�§</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>ç�»å½�</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">ç�»å½�</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">æ�´å¤�产å��</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>å
      

³äº�ç�¾åº¦</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使ç�¨ç�¾åº¦å��å¿
读</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>æ��è§�å��é¦�</a> äº¬ICPè¯�030173å�·  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

 ```
  1. 因此这段代码的作用便是将百度页面的源代码输出来
  • requests的具体使用

    1. 刚才那个例子使用的是get方法,我们请求的方法有很多,包括get,post,put等方式,但是使用再多的还是get和post方法,因此我们将重点学习get和post方式的请求。

    2. 有的时候我们需要向服务器传递参数,get方法传递参数的方式有以下几种方式

      import requests
      url="https://baidu.com?a=sun&b=one"
      data=requests.get(url)
      print(data.text)
      

      观察一下这段代码和刚才的有哪些不同,这段代码传递了两个参数a和b,我们可以直接将参数写在url中来传递以get方法传递的参数,这个参数传递后服务器便可以接受我们传递过去的参数并加以处理。

      import requests
      url="https://baidu.com"
      param={
          'a':'sun',
          'b':'one'
          }
      data=requests.get(url,params=param)
      print(data.text)
      

      还有另一种传递参数的方式,观察上面的代码,可以发现,我们先创建一个字典,然后将a和b当作键,sun和one当作值。然后在请求里使用params=param来传递一个字典,这种方式传递参数和上面那个的效果是等同的,只是方式不同,牢记params这个参数是固定的。

  1. 使用post方法传递参数

    观察以下代码

    import requests
    url="https://baidu.com"
    data={
        'a':'sun',
        'b':'one'
        }
    data=requests.post(url,data=data)
    print(data.text)
    

    可以看到使用post方法和get的第二种方式有点类似,都是先创建一个字典,然后将需要传递的参数写入字典中,但是我们在请求时使用的是requests.post(),并且使用data=data的方式传递参数,这个data参数和params一样是固定的。

  1. post和get方法的不同

    使用get方法传递参数其实是不安全的,浏览器会将get传递的参数在网址中显示出来,并且get方法传递的参数的大小是有限制的。

 使用post方法传递参数是不限制数据的大小,并且不会再浏览器的网址上显示出我们要传递的参数,因此是比较安全的,我们传递的大部分数据都是使用post方式提交的,比如表单中的数据。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值