本节内容

  1. 模块的安装

    -----------------------基础用法---------------------

  2. GET用法、POST用法

    -----------------------进阶用法--------------------

  3. cookie处理、代理ip、session

一 模块安装:

1). 安装requests包还是很方便的,电脑中有python环境,打开cmd,输入pip install requests下载;

如果有同学使用pycharm的话,选择file-->setting-->Project interpreter-->右边"+"号点击--->输入模块名---->选中下载。

2). requests的作用、特点、以及使用流程

  • 作用:模拟用户使用浏览器上网

  • 特点:简单、高效

  • 使用流程:

    1. 指定url;

    2. 发起请求(requests.get/post);

    3. 获取响应信息/数据(response);

    4. 持久化存储(保存csv、MySQL、txt等);

 

二基本用法:

1). get(url,headers,params):各用法

获取搜狗首页的页面数据:

复制代码
 1 import requests #引包 2 #1指定url 3 url = 'https://www.sogou.com/' 4 #2.发起请求 5 response = requests.get(url=url)  6 #3获取响应数据 7 page_text = response.text #text返回的是字符串类型的数据 8 #持久化存储 9 with open('./sogou.html','w',encoding='utf-8') as fp: 10     fp.write(page_text) 11 print('over!') 12 #也可以直接打印13 print(page_text)   #这就是服务器给我们返回的数据信息(response)
复制代码