> 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/products/cn/headless-browser/features/geolocation-targeting.md).

# 地理位置定向

你可以通过添加以下内容来为你的无头浏览器会话指定位置： `p_cc`, `p_city`，或 `p_state` 参数到你的连接 URL：

* `p_cc` – 选择国家，使用 2 位国家代码（例如 US、DE、FR）。如果未指定国家，系统将根据可用性自动分配一个。
* `p_city` – 选择以小写字母书写的城市（例如 berlin、los\_angeles 等）。你还必须指定以下其中之一： `p_cc` 或 `p_state`
* `p_state` – 选择一个州（仅限美国），当以小写名称使用时（例如 texas、ohio）

以下是国家定向的代码示例：

{% tabs %}
{% tab title="Python（Playwright）" %}

```python
from playwright.sync_api import sync_playwright

username = "your-username"
password = "your-password"
endpoint = "ubc.oxylabs.io"
cc = "US"  # 替换为所需的 2 位国家代码
browser_url = f"wss://{username}:{password}@{endpoint}?p_cc={cc}"

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp(browser_url)
    page = browser.new_page()
    page.goto('https://example.com')
    browser.close()
```

{% endtab %}

{% tab title="JavaScript（Playwright）" %}

```javascript
import { chromium } from 'playwright';

(async () => {
    const username = 'your-username';
    const password = 'your-password';
    const endpoint = 'ubc.oxylabs.io';
    const cc = 'US';  // 替换为所需的 2 位国家代码
    const browserUrl = `wss://${username}:${password}@${endpoint}?p_cc=${country}`;

    const browser = await chromium.connectOverCDP(browserUrl);
    const page = await browser.newPage();
    await page.goto('https://example.com');
    await browser.close();
})();
```

{% endtab %}

{% tab title="JavaScript（Puppeteer）" %}

```javascript
import puppeteer from 'puppeteer';

(async () => {
    const username = 'your-username';
    const password = 'your-password';
    const endpoint = 'ubc.oxylabs.io';
    const cc = 'US';  // 替换为所需的 2 位国家代码
    const browserUrl = `wss://${username}:${password}@${endpoint}?p_cc=${country}`;

    const browser = await puppeteer.connect({
        browserWSEndpoint: browserUrl
    });
    const page = await browser.newPage();
    await page.goto('https://example.com');
    await browser.close();
})();
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
**注意：** 如果 `p_state` 和 `p_cc` 都已指定， `p_state` 将优先使用，并且 `p_cc` 会被忽略。
{% endhint %}


---

# 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 dynamically by asking a question.

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

```
GET https://developers.oxylabs.io/products/cn/headless-browser/features/geolocation-targeting.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.
