python爬虫入门篇:requests的基本使用

一、安装

  • pip快速安装
pip install requests

二、使用方法

1)导入模块

import requests

2)发送请求

  • 示例get请求:以百度搜索get请求为例
import requests

# 参数直接拼接到url的get请求
r1 = requests.get('https://www.baidu.com/s?wd=hello%20world')   
# 参数放到params的get请求(两种get请求方法最终发送的url都是https://www.baidu.com/s?wd=hello%20world)
r2 = requests.get(url='https://www.baidu.com/s', params={'wd': 'hello%20world'})      
  • 常用请求方式
# GET请求
requests.get('https://code_space/get') 
# POST请求
requests.post('https://code_space/post')  
# PUT请求
requests.put('https://code_space/put')
# DELETE请求
requests.delete('https://code_space/delete')

3)定制头和cookie信息

cookie= {'key': 'value'}
header = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "Host": "ug.baidu.com",
    "Origin": "https://www.baidu.com",
    "Referer": "https://www.baidu.com/s?ie=UTF-8&wd=hello%20world",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
}
r = requests.get('https://www.baidu.com/s?wd=hello%20world',headers=header,cookies=cookie) 

4)获取响应

r.encoding				# 获取当前的编码
r.encoding = 'utf-8'	# 设置编码
r.text					# 以encoding解析返回内容。字符串方式的响应体,会自动根据响应头部的字符编码进行解码。
r.content				# 以字节形式(二进制)返回。字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩。
r.headers				# 以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
r.status_code			# 响应状态码
r.raw					# 返回原始响应体,也就是 urllib 的 response 对象,使用 r.raw.read()   
r.o						# 查看r.ok的布尔值便可以知道是否登陆成功
 
 #*特殊方法*#
r.json()				#Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的,不然解析出错会抛异常
r.raise_for_status()	#失败请求(非200响应)抛出异常
  • 使用requests方法后,会返回一个response对象,其存储了服务器响应的内容,如上实例中已经提到的 r.text、r.status_code……
  • 获取文本方式的响应体实例:当你访问 r.text 之时,会使用其响应的文本编码进行解码,并且你可以修改其编码让 r.text 使用自定义的编码进行解码。

三、测试demo

# -*- coding: utf-8 -*-
"""
@Time : 2022/1/16 16:15
@Auth : 技术空间
@File :requests_demo.py
@IDE :PyCharm
@Motto:技术总是要日积月累的

"""
import requests

if __name__ == '__main__':
    r1 = requests.get('https://www.baidu.com/s?wd=hello%20world')

    cookie = {'key': 'value'}
    header = {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "Host": "ug.baidu.com",
        "Origin": "https://www.baidu.com",
        "Referer": "https://www.baidu.com/s?ie=UTF-8&wd=hello%20world",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                      "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
    }
    r2 = requests.get('https://www.baidu.com/s?wd=hello%20world', headers=header, cookies=cookie)
    print("r1的status_code-->" + str(r1.status_code))
    print(r1.text)
    print("r2的status_code-->" + str(r1.status_code))
    print(r2.text)

在这里插入图片描述在这里插入图片描述

四、其它拓展

关于requests的post方法使用我将会在下一篇中讲述如何使用以及注意细节。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_space

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值