> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/integrations/cn/proxy-integrations/puppeteer.md). Agents can also search and read this documentation through its [MCP server](https://developers.oxylabs.io/~gitbook/mcp).

# Puppeteer

使用 Oxylabs 代理设置 Puppeteer，以通过 Node.js 控制无头 Chrome 或 Chromium。带代码示例的分步指南。

[**Puppeteer**](https://developer.chrome.com/docs/puppeteer/) 是一个 Node 库，提供高级 API，用于通过 DevTools Protocol 控制无头 Chrome 或 Chromium。

观看我们的视频指南，或按照以下步骤将 Puppeteer 与以下 Oxylabs 代理集成：

* [**住宅和移动代理**](#residential-and-mobile-proxies)
* [**数据中心代理**](#datacenter-proxies)
* [**独享数据中心代理（企业版）**](#dedicated-datacenter-proxies-enterprise)
* [**独享数据中心代理（自助版）**](#dedicated-datacenter-proxies-self-service)
* [**ISP代理**](#isp-proxies)
* [**独享ISP代理**](#dedicated-isp-proxies)

{% embed url="<https://youtu.be/9xyvfs0Qikw>" %}

### **步骤 1**

安装 [Node.js](https://nodejs.org/en/download) 以及你选择的代码编辑器。（如果你已经有了，可跳过此步骤）

### **步骤 2**

创建一个 Node.js 项目并安装所需的包。

### **步骤 3**

在 Puppeteer 中，填入该值，例如：

在你的 Puppeteer 脚本中，设置 `--proxy-server` 值，如下所示：

```javascript
(async () => {
    const browser = await puppeteer.launch({
        headless: false,
        args: [`--proxy-server=host:port`]
    });
```

输入你的 `server` 值，具体取决于你选择的 Oxylabs 代理类型：

* [住宅和移动代理](#residential-and-mobile-proxies)
* [数据中心代理](#datacenter-proxies)
* [独享数据中心代理（企业版）](#dedicated-datacenter-proxies-enterprise)
* [独享数据中心代理（自助版）](#dedicated-datacenter-proxies-self-service)
* [ISP代理](#isp-proxies)
* [独享ISP代理](#dedicated-isp-proxies)

#### 住宅和移动代理

```javascript
args: [`--proxy-server=pr.oxylabs.io:7777`] //--proxy-server=host:port
```

**主机：** `pr.oxylabs.io`

**端口：** `7777`

**用户名：** `customer-USERNAME`

**密码：** `PASSWORD`

{% hint style="warning" %}
**注意：** 别忘了添加 `customer-` 前缀到您的用户名中。
{% endhint %}

你还可以使用 **附加参数** 与住宅和移动代理一起使用，以设置地理位置（国家、州、城市等）并控制会话。请查看 [住宅代理文档](https://developers.oxylabs.io/products/cn/proxies/residential-proxies.md) 了解更多。

#### 数据中心代理

```javascript
args: [`--proxy-server=dc.oxylabs.io:8001`] //--proxy-server=host:port
```

**主机：** `dc.oxylabs.io`

**端口：** `8001`

**用户名：** `user-username`

**密码：** `password`

{% hint style="warning" %}
**注意：** 别忘了添加 `user-` 前缀到您的用户名中。
{% endhint %}

**按 IP 计费订阅**

端口是分配给列表中 IP 地址的顺序编号 [代理列表](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/proxy-list.md)的 IP 地址的顺序编号。因此，端口 `8001` 会使用你列表中的第一个 IP 地址。你还可以通过将端口设置为 [代理 IP 轮换](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/ip-control.md) 来启用 `8000`.

此外，你还可以指定 [地理位置](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/select-country.md)，例如美国，方法是将 **代理用户名** 设置为 `user-USERNAME-country-US`.

**按流量计费订阅**

端口 `8001` 会随机选取一个 IP 地址，但在会话期间保持一致。或者，你可以 [轮换代理 IP](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/ip-control.md) ，通过使用端口 `8000`.

如果你想使用特定的 [地理位置](https://developers.oxylabs.io/products/cn/proxies/datacenter-proxies/select-country.md)，例如美国，请将代理用户名设置为 `user-USERNAME-country-US`.

#### 独享数据中心代理（企业版）

```javascript
args: [`--proxy-server=1.2.3.4:60000`] //--proxy-server=host:port
```

**主机：** `1.2.3.4` （来自你的 [代理列表](https://developers.oxylabs.io/products/cn/proxies/dedicated-datacenter-proxies/enterprise/proxy-lists.md))

**端口：** `60000` （HTTP）， `1180` （SOCKS5）

**用户名：** `username`

**密码：** `password`

对于企业独享数据中心代理（通过销售团队购买），你必须从已获得的 [代理列表](https://developers.oxylabs.io/products/cn/proxies/dedicated-datacenter-proxies/enterprise/proxy-lists.md).

#### 独享数据中心代理（自助版）

```javascript
args: [`--proxy-server=ddc.oxylabs.io:8001`] //--proxy-server=host:port
```

**主机：** `ddc.oxylabs.io`

**端口：** `8001`

**用户名：** `user-username`

**密码：** `password`

{% hint style="warning" %}
**注意：** 别忘了添加 `user-` 前缀到您的用户名中。
{% endhint %}

对于自助服务独享数据中心代理（通过 Oxylabs 控制面板购买），端口表示来自已获得的 IP 地址的顺序编号 [代理列表](https://developers.oxylabs.io/products/cn/proxies/dedicated-datacenter-proxies/self-service/proxy-list.md)中某个 IP 地址的顺序编号。如果你想让每个请求使用不同的 IP，请将端口改为 `8000` 以启用 [代理轮换](https://developers.oxylabs.io/products/cn/proxies/dedicated-datacenter-proxies/self-service/proxy-rotation.md).

#### ISP代理

```javascript
args: [`--proxy-server=isp.oxylabs.io:8001`] //--proxy-server=host:port
```

**主机：** `isp.oxylabs.io`

**端口：** `8001`

**用户名：** `user-username`

**密码：** `password`

{% hint style="warning" %}
**注意：** 别忘了添加 `user-` 前缀到您的用户名中。
{% endhint %}

对于 ISP 代理，端口会分配给特定的 IP 地址及其位置。因此，端口 `8001` 将始终选取你 [代理列表](https://developers.oxylabs.io/products/cn/proxies/isp-proxies/proxy-list.md)中某个 IP 地址的顺序编号。如果你想让每个请求使用不同的 IP，请将端口改为 `8000` 以启用 [代理轮换](https://developers.oxylabs.io/products/cn/proxies/isp-proxies/proxy-rotation.md).

#### 独享ISP代理

```javascript
args: [`--proxy-server=disp.oxylabs.io:8001`] //--proxy-server=host:port
```

**主机：** `disp.oxylabs.io`

**端口：** `8001`

**用户名：** `user-username`

**密码：** `password`

{% hint style="warning" %}
**注意：** 别忘了添加 `user-` 前缀到您的用户名中。
{% endhint %}

你需要选择分配给已购买的 IP 地址的端口， [代理列表](https://developers.oxylabs.io/products/cn/proxies/dedicated-isp-proxies/self-service/proxy-list.md)，你可以在 Oxylabs 控制面板中找到。 `8001` 端口会为所有请求从你的列表中选取第一个 IP 地址。使用端口 `8000` 用于自动 [代理 IP 轮换](https://developers.oxylabs.io/products/cn/proxies/dedicated-isp-proxies/self-service/proxy-rotation.md).

### **步骤 4**

在……下方 `page.authenticate()`，输入你的 Oxylabs 代理用户的 `username` 和 `password`。一个基本的代码示例如下：

```javascript
const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    args: ['--proxy-server=pr.oxylabs.io:7777]  
});
  const page = await browser.newPage();
    await page.authenticate({
        username: 'USERNAME',
        password: 'PASSWORD'
    });
    await page.goto('https://ip.oxylabs.io');
    await page.screenshot({path: 'example.png'});
    await browser.close();
})();
```

{% hint style="warning" %}
**注意：** 别忘了添加 `customer-` 或 `user-` 如果所选代理类型需要，请在用户名前加上前缀。
{% endhint %}

就是这样！你已经成功将 Oxylabs 代理与 Puppeteer 集成。你还可以在原始的 Puppeteer 集成博客文章中找到 `proxy-chain` 教程以及轮换代理指南 [这里](https://oxylabs.io/resources/integrations/puppeteer).

如果你想进一步了解使用 Puppeteer 进行网页抓取，请阅读我们的 [博客文章](https://oxylabs.io/blog/puppeteer-tutorial).

### 常见错误

<details>

<summary><strong>Puppeteer 返回错误</strong></summary>

你可能遇到的一个问题是，当你尝试连接到代理服务器时，Puppeteer 返回错误。其原因可能多种多样，主要原因是你的代理服务器需要使用代理凭据进行身份验证。

要解决这个问题，你需要在 `Puppeteer 选项` 对象中输入代理服务器的用户名和密码。之后，再尝试使用代理。

</details>

<details>

<summary><strong>页面无法加载</strong></summary>

尽管 Puppeteer 可能能够连接到代理，但它仍然可能在加载页面时遇到问题。最常见的原因是网络连接较差。

解决此问题的最常见方法是增加 `Puppeteer 选项` 对象中的超时时间。完成后，使用代理应该不再是问题。

</details>

<details>

<summary><strong>其他问题</strong></summary>

如果问题仍未解决，这意味着由于某种原因，你的代理无法维持连接。在这种情况下，你应该检查代理服务器，或联系你的提供商以获得进一步帮助。

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.oxylabs.io/integrations/cn/proxy-integrations/puppeteer.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
