requests 之 get、post 用法

urllib 库的在处理网页验证和Cookie时需要写Opener类和Handler类来处理。另外实现POST、PUT等请求时的写法也不太方便。为了更加方便地实现这些操作,产生了更为强大的库-requests。

requests库在是实现网页请求的时候,可以用一行代码进行实现,如: 以get方法实现 GET 方式请求网页,使用方法如下:

# *******************************
#   use method of requests.get
# *******************************
import requests
url = 'https://www.baidu.com/'
response = requests.get(url)    # requests中 get 方法返回一个Response对象。
print(type(response))    # 输出响应的类型
print(response.status_code)    # 输出响应的状态码
print(response.text)    # 输出响应的内容
print(response.cookies)    # 输出响应的 cookies

另外,requests 的其他请求类型类似:

response = requests.get('https://www.baidu.com/')
response = requests.post('https://www.baidu.com/')
response = requests.put('https://www.baidu.com/')
response = requests.delete('https://www.baidu.com/')
response = requests.patch('https://www.baidu.com/')

1、GET 请求

构建 GET 请求,请求链接为 https://www.httpbin.org/get。

import requests
data = {
    'user':'admin',
    'password':'123456'
}
url = 'https://www.httpbin.org/get'
response = requests.get(url=url, params=data)
print(response.text)

运行输出结果:

{
  "args": {
    "password": "123456",
    "user": "admin"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "www.httpbin.org",
    "User-Agent": "python-requests/2.31.0",
    "X-Amzn-Trace-Id": "Root=1-64c37f84-6d1831f3167373e87cea683e"
  },
  "origin": "222.17.11.22",
  "url": "https://www.httpbin.org/get?user=admin&password=123456"
}

GET请求返回结果为json格式,可以通过如下方式进行解析:

json = response.json()
print(json['args'])

输出结果:

{'password': '123456', 'user': 'admin'}

2、GET 请求抓取网页内容

GET 请求普通网页,可以抓取信息,使用示例如下:

import requests
import re
url = 'https://ssr1.scrape.center/'
response = requests.get(url)
pattern = re.compile('<h2.*?>(.*?)</h2>',re.S)
h2 = re.findall(pattern=pattern, string=response.text)
print(h2)

运行输出结果:

['霸王别姬 - Farewell My Concubine', '这个杀手不太冷 - Léon', '肖申克的救赎 - The Shawshank Redemption', '泰坦尼克号 - Titanic', '罗马假日 - Roman Holiday', '唐伯虎点秋香 - Flirting Scholar', '乱世佳人 - Gone with the Wind', '喜剧之王 - The King of Comedy', '楚门的世界 - The Truman Show', '狮子王 - The Lion King']

3、GET 请求抓取二进制数据

GET 请求抓取二进制数据:图片、音视频等,使用示例如下:

# ***************************************************
#   use method of requests.get: get binary data
# ***************************************************
import requests
url = 'https://scrape.center/favicon.ico'
response = requests.get(url)
print(response.text)
print(response.content)

由于图片是二进制数据,response.text 在打印时会直接转化为字符串str类型,会出现乱码;response.content 是 bytes 类型的数据,前面会带有一个'b'。将二进制图片保存的方式如下:

# ***************************************************
#   use method of requests.get: get binary data, and save as picture
# ***************************************************
import requests
url = 'https://scrape.center/favicon.ico'
response = requests.get(url)
save_file = 'favicon.ico'
with open(save_file, 'wb') as file:
    file.write(response.content)

这里使用open方法进行文件读写,open的用法 open(filename, mode), filename:要打开的文件名(字符串类型), mode:打开文件的模式(字符串类型),可选参数,默认为'r'(只读模式),'wb'为二进制方式写入。

4、GET 添加请求头

实际应用中,会在请求时添加请求头信息headers,将请求伪装为正常浏览器发起的请求。添加请求头方法如下:

# ***************************************************
#   use method of requests.get: add headers
# ***************************************************
import requests
headers = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko)'
                 'Chrome/52.0.2743.116 Safari/ 537.36'
}
url = 'https://ssr1.scrape.center/'
response = requests.get(url=url, headers=headers)
print(response.text)

5、POST 请求

使用 requests 实现 POST 方式请求,方法如下:

# ***************************************************
#   use method of requests.post
# ***************************************************
import requests
data = {
    'username':'admin',
    'password':'123456'
}
url = 'https://www.httpbin.org/post'
response = requests.post(url=url, data=data)
print(response.text)

后续公众号会发布系列教程,更多内容请关注公众号:程序猿学习日记 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序猿学习

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

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

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

打赏作者

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

抵扣说明:

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

余额充值