For the complete documentation index, see llms.txt. This page is also available as Markdown.

请求头和 Cookie

了解如何使用自定义请求头和 Cookie 提高网站访问率。

网页解锁器通过使用我们团队预定义的请求头和Cookie来优化数据访问。

自定义请求头

如果你需要使用自己的请求头,可以通过添加 x-oxylabs-force-headers: 1 将其添加到你的请求中。网页解锁器会把你的自定义请求头合并到预定义的请求头集合中,然后用它来访问网站。

代码示例

curl -k -v -x https://unblock.oxylabs.io:60000 \
-U 'USERNAME:PASSWORD' \
'https://ip.oxylabs.io/headers' \
-H 'x-oxylabs-force-headers: 1' \
-H 'Accept-Language: en-GB' \
-H 'Your-Header-Name: interesting header content'
import requests

# 在此使用你的网页解锁器凭据。
USERNAME, PASSWORD = 'YOUR_USERNAME', 'YOUR_PASSWORD'

# 定义代理字典。
代理 = {
  'http': f'http://{USERNAME}:{PASSWORD}@unblock.oxylabs.io:60000',
  'https': f'https://{USERNAME}:{PASSWORD}@unblock.oxylabs.io:60000',
}

headers = {
    'x-oxylabs-force-headers': '1',
    'Accept-Language': 'en-GB',
    'Your-Header-Name': 'interesting header content'
}

response = requests.get(
    'https://ip.oxylabs.io/headers',
    verify=False,  # 需要忽略证书
    代理=代理,
    headers=headers,
)

# Print result page to stdout
print(response.text)

# Save returned HTML to result.html file
with open('result.html', 'w') as f:
    f.write(response.text)

如果你需要使用自己的 Cookie,可以通过添加以下内容 x-oxylabs-force-Cookie:1 请求中的标头。网页解锁器会将你的自定义 Cookie 合并到预定义的 Cookie 集中,然后用它来访问网站。

代码示例

curl -k -v -x https://unblock.oxylabs.io:60000 \
-U 'USERNAME:PASSWORD' \
'https://example.com' \
--Cookie 'NID=1234567890' \
-H 'x-oxylabs-force-Cookie: 1'
import requests

# 在此使用你的网页解锁器凭据。
USERNAME, PASSWORD = 'YOUR_USERNAME', 'YOUR_PASSWORD'

# 定义代理字典。
代理 = {
  'http': f'http://{USERNAME}:{PASSWORD}@unblock.oxylabs.io:60000',
  'https': f'https://{USERNAME}:{PASSWORD}@unblock.oxylabs.io:60000',
}

headers = {
    'Cookie': 'NID=1234567890',
    'x-oxylabs-force-Cookie': '1' 
}

response = requests.get(
    'https://example.com',
    代理=代理,
    headers=headers,
    verify=False  # 忽略证书验证
)

# Print result page to stdout
print(response.text)

# Save returned HTML to result.html file
with open('result.html', 'w') as f:
    f.write(response.text)

最后更新于

这有帮助吗?