python requests模块4种传参方式

Requests模块是第三方模块,需要预先安装,requests模块在python内置模块的基础上进行了高度的封装,从而使得python进行网络请求时,变得更加简洁和人性化。

 

一、get请求

1.1 发送带参数的请求

你也许经常想为 URL 的查询字符串(query string) 传递某种数据。如果你是手工构建 URL,那么数据会以键/值对的形式置于 URL 中,跟在一个问号的后面。例如, www.baidu.com/?key=val。

Requests 允许你使用 params 关键字参数,以一个字符串字典来提供这些参数。

举例来说,如果你想传递 key1=value1 和 key2=value2 到 www.baidu.com/ ,那么你可以使用如下代码:

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("https://www.baidu.com/", params=payload)
print(r.url)
https://www.baidu.com/?key2=value2&key1=value1

 如果你想为请求添加 HTTP 头部,只要简单地传递一个 dict 给 headers 参数就可以了

url = 'https://www.baidu.com/s?wd=python'
headers = {
        'Content-Type': 'text/html;charset=utf-8',
        'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
    }
r = requests.get(url,headers=headers)

 

二、post请求

HTTP 协议规定 POST 提交的数据必须放在消息主体(entity-body)中,但协议并没有规定数据必须使用什么编码方式,服务端通过是根据请求头中的Content-Type字段来获知请求中的消息主体是用何种方式进行编码,再对消息主体进行解析。具体的编码方式包括

以form表单形式提交数据

application/x-www-form-urlencoded

以json串提交数据

application/json

一般使用来上传文件

multipart/form-data

 

2.1、以form形式发送post请求

Reqeusts支持以form表单形式发送post请求,只需要将请求的参数构造成一个字典,然后传给requests.post()的data参数即可

payload = {'key1': 'value1',
           'key2': 'value2'
           }
r = requests.post("http://httpbin.org/post", data=payload)

 

2.2、以json形式发送post请求

可以将一 json串传给requests.post()的data参数

url = 'http://httpbin.org/post'
payload = {'key1': 'value1', 'key2': 'value2'}

r = requests.post(url, json=payload)

 

2.3、以multipart形式发送post请求

Requests也支持以multipart形式发送post请求,只需将一文件传给requests.post()的files参数即可

>>> url = 'http://httpbin.org/post'
>>> files = {'file':open('fang.txt','rb')}
>>> r3 = requests.post(url,files=files)
>>> r3.request.headers
{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '164', 'Content-Type': 'multipart/form-data; boundary=5618f94218afb024124625245ed0df12'}
>>>
>>> r3.request.body
b'--5618f94218afb024124625245ed0df12\r\nContent-Disposition: form-data; name="file"; filename="wang.txt"\r\n\r\n\\[System\\] \r\n1234 \r\n\r\n--5618f94218afb024124625245ed0df12--\r\n'

 

 

三、实例

3.1、下载单张图片并保存到本地磁盘目录.

#!/usr/bin/env python
#coding:utf-8

import requests
import os

# 下载图片URL
url = 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'

# 保存地址
path = "E://图片//"

# 构造下载图片url
down = path + url.split('/')[-1]
#print(down)
# E://图片//bd_logo1_31bdc765.png
try:
    # 判断目录是否存在
    if not os.path.exists(path):
        os.mkdir(path)
    # 如果url不存在,则开始下载
    if not os.path.exists(down):
        r = requests.get(url)
        print(r)
        # 开始写文件,wb代表写二进制文件
        with open(down,'wb') as f:
            # 图片以二进制形式保存(r.content)
            f.write(r.content)
        print("图片下载成功")
    else:
        print("图片已经存在.")

except Exception as e :
    print("爬取失败:",str(e))

 

3.2、使用requests模块和bs4模块,抓取贴吧图片并保存到指定目录

#!/usr/bin/env python
#coding:utf-8

import requests
from bs4 import BeautifulSoup
import os

#图片保存路径:
path = "E://爬虫专用//"

URL = 'http://tieba.baidu.com/p/1753935195'
html_page = requests.get(URL)

#创建BeautifulSoup对象
soup = BeautifulSoup(html_page.text,'html.parser')

#通过class="BDE_Image"获取所有的img 标签
class_image = soup.findAll(attrs={"class":"BDE_Image"})
print(class_image)

#判断目录是否存在
if not os.path.exists(path):
    os.mkdir(path)
try:
    x = 0
    # 循环class_image列表,找到所有img标签的链接
    for i in class_image:
        #取出src对应的url地址
        src_url = i.get('src')
        #请求src_url链接地址
        imge_list = requests.get(src_url)
        #构造url名称
        #down = path + src_url.split('/')[-1]
        down = path + '%s.jpg' %x
        print(down)
        #以二进制保存图片
        with open(down,'wb') as f:
            f.write(imge_list.content)
        x += 1

except Exception as e:
    print("pass")

 

posted @ 2022-06-29 15:56  超级宝宝11  阅读(1449)  评论(0编辑  收藏  举报