# 如何实现模拟登录？

### 如何实现模拟登录？ <a href="#ru-he-shi-xian-mo-ni-deng-lu" id="ru-he-shi-xian-mo-ni-deng-lu"></a>

> 通过模拟登录, 可以解决登录后才能爬取某些网站数据的问题.\
> PHPSpider框架提供两种登录方式：
>
> > 1、通过发送HTTP请求来实现模拟登录\
> > 2、从Chrome浏览器拷贝Cookie字符串

#### 通过发送HTTP请求来实现模拟登录 <a href="#tong-guo-fa-song-http-qing-qiu-lai-shi-xian-mo-ni-deng-lu" id="tong-guo-fa-song-http-qing-qiu-lai-shi-xian-mo-ni-deng-lu"></a>

举个栗子:

```
// 登录请求url
$login_url = "http://www.waduanzi.com/login?url=http%3A%2F%2Fwww.waduanzi.com%2F";
// 提交的参数
$params = array(
    "LoginForm[returnUrl]" => "http%3A%2F%2Fwww.waduanzi.com%2F",
    "LoginForm[username]" => "13712899314",
    "LoginForm[password]" => "854230",
    "yt0" => "登录",
);
// 发送登录请求
requests::post($login_url, $params);
// 登录成功后本框架会把Cookie保存到www.waduanzi.com域名下，我们可以看看是否是已经收集到Cookie了
$cookies = requests::get_cookies("www.waduanzi.com");
print_r($cookies);  // 可以看到已经输出Cookie数组结构

// requests对象自动收集Cookie，访问这个域名下的URL会自动带上
// 接下来我们来访问一个需要登录后才能看到的页面
$url = "http://www.waduanzi.com/member";
$html = requests::get($url);
echo $html;     // 可以看到登录后的页面，非常棒👍
```

#### `如何获得提交参数？` <a href="#ru-he-huo-de-ti-jiao-can-shu" id="ru-he-huo-de-ti-jiao-can-shu"></a>

登录需要登录验证信息，下面我们来看看如何获得一个网站所需要的登录信息\
还是以挖段子([www.waduanzi.com)为例，看看如何获得下面的信息](http://www.waduanzi.com\)为例，看看如何获得下面的信息)

**1、打开挖段子网站点击登录按钮进入登陆页：**\
`http://www.waduanzi.com/login?url=http%3A%2F%2Fwww.waduanzi.com%2F`

**2、鼠标点击右键 -> 检查 从而打开Chrome浏览器的开发者工具**

![](/files/nLanA0SxiwZmD5EnONFx)

**选择Network选项卡，勾选Preserve log选项**

![](/files/NYtj6FDhUvdHnKDw7TMS)

**3、填写登陆信息点击登录按钮，得到登录验证URL**

![](/files/QL1zbFW62yYN71j4bZGz)

**4、上面的登录提交字段填入框架代码**

```
$params => array(
    "LoginForm[returnUrl]" => "http%3A%2F%2Fwww.waduanzi.com%2F",
    "LoginForm[username]" => "用户名",
    "LoginForm[password]" => "密码",
    "yt0" => "登录",
)
```

#### 从Chrome浏览器拷贝Cookie字符串 <a href="#cong-chrome-liu-lan-qi-kao-bei-cookie-zi-fu-chuan" id="cong-chrome-liu-lan-qi-kao-bei-cookie-zi-fu-chuan"></a>

上面的方式适用于简单的登录验证方式，如果遇到验证码，token表单字段，还有各种用js加密算法生成的登录字段，模拟登录就会变得异常复杂，为了节省时间，我们一般人工登录后，拷贝Cookie字符串来登录

**1、登录成功后，在跳转的页面找Cookie字符串**

![](/files/gXEc0UhUPsVqstgmR0Yl)

**2、上面的登录提交字段填入框架代码**

```
// 模拟登录
$cookies = "复制上面Cookie:后面那一串字符串...";
requests::set_cookies($cookies, 'www.waduanzi.com');

// 接下来我们来访问一个需要登录后才能看到的页面
$url = "http://www.waduanzi.com/member";
$html = requests::get($url);
echo $html;     // 可以看到登录后的页面，非常棒👍
```

### results matching ""

*

### No results matching ""


---

# 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://zhai-shi-sansorganization.gitbook.io/phpspider/ru-he-shi-xian-mo-ni-deng-lu.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.
