# 3.1  多功能的 Requests

* post

1.账号登录

2.搜索内容

3.上传图片

4.上传文件

5.往服务器传数据 等

* get

1.正常打开网页

2.不往服务器传数据

## requests get 请求

将parameters用字典代替, 然后传入 requests.get() 功能，用webbrowser模块打开一个默认浏览器, 观看在百度的搜索页面.

```python
import requests
import webbrowser
param = {"wd": "莫烦Python"}  # 搜索的信息
r = requests.get('http://www.baidu.com/s', params=param)
print(r.url)
webbrowser.open(r.url)

# http://www.baidu.com/s?wd=%E8%8E%AB%E7%83%A6Python
```

## requests post 请求

填入姓名的地方是在一个 < form> 里面

![](https://morvanzhou.github.io/static/results/scraping/3-1-8.png)

< form> 里面有一些 < input> 的tag, < input> 里面的 name="firstname"和 name="lastname"就是要post提交上去的关键信息

```python
data = {'firstname': '暴走', 'lastname': '林'}
r = requests.post('http://pythonscraping.com/files/processing.php', data=data)
print(r.text)

# Hello there, 暴走 林!
```

### 上传图片

传照片是 post 的一种

![](https://morvanzhou.github.io/static/results/scraping/3-1-10.png)

传送完照片以后的 url 有变动，“choose file” 按键链接的 < input> 是一个叫 uploadFile 的名字，将这个名字放入 python 的字典当一个 “key”

![](https://morvanzhou.github.io/static/results/scraping/3-1-11.png)

在字典中, 使用 open 打开一个图片文件, 当做要上传的文件，把这个字典放入 post 里面的 files 参数. 就能上传图片，网页会返回一个页面, 将图片名显示在上面.

```python
file = {'uploadFile': open('./image.png', 'rb')}
r = requests.post('http://pythonscraping.com/files/processing2.php', files=file)
print(r.text)

# The file image.png has been uploaded.
```

### 登录

![](https://morvanzhou.github.io/static/results/scraping/3-1-12.png)

![](https://morvanzhou.github.io/static/results/scraping/3-1-13.png)

登录账号, 浏览器做了:

1.使用 post 方法登录了第一个红框的 url

2.post 的时候, 使用了 Form data 中的用户名和密码

3.生成了一些 cookies

因为打开网页时, 每一个页面都是不连续的, 没有关联的, cookies 就是用来衔接一个页面和另一个页面的关系

用 requests.post + payload 的用户信息发给网页, 返回的 r 里面会有生成的 cookies 信息.

请求去登录后的页面时, 使用 request.get, 并将之前的 cookies 传入到 get 请求. 就能以登录的名义访问 get 的页面.

```python
payload = {'username': 'Morvan', 'password': 'password'}
r = requests.post('http://pythonscraping.com/pages/cookies/welcome.php', data=payload)
print(r.cookies.get_dict())

# {'username': 'Morvan', 'loggedin': '1'}


r = requests.get('http://pythonscraping.com/pages/cookies/profile.php', cookies=r.cookies)
print(r.text)

# Hey Morvan! Looks like you're still logged into the site!
```

![](https://morvanzhou.github.io/static/results/scraping/3-1-14.png)

### 使用 Session 登录

创建完一个 session 过后, 直接只用 session 来 post 和 get.

而且这次 get 的时候, 并没有传入 cookies. 但是实际上 session 内部

就已经有了之前的 cookies

```python
session = requests.Session()
payload = {'username': 'Morvan', 'password': 'password'}
r = session.post('http://pythonscraping.com/pages/cookies/welcome.php', data=payload)
print(r.cookies.get_dict())

# {'username': 'Morvan', 'loggedin': '1'}


r = session.get("http://pythonscraping.com/pages/cookies/profile.php")
print(r.text)

# Hey Morvan! Looks like you're still logged into the site!
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/python/geng-duo-qing-qiu-xia-zai-fang-shi/duo-gong-neng-de-requests.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
