Documentation has been updated: see help center and changelog in one place.

下载图片

您可以使用 网页爬虫API (Web Scraper API) 下载 jpeg、svg 和 png 格式的图像。

如果您通过 Proxy Endpoint这样做,您可以简单地将输出保存为图像扩展名,例如:

curl -k -x realtime.oxylabs.io:60000 -U "USERNAME:PASSWORD" "https://sandbox.oxylabs.io/assets/action-adventure.svg" >> image.svg

如果您正在使用 Push-PullRealtime 集成方法,您需要添加 content_encoding 参数,值为 base64。一旦收到结果,您需要将从 内容 编码的数据显示解码为字节并保存为图像文件。

请在下面找到一个 Python 示例。

import base64
import json
import requests

# 您的凭据。
USERNAME = ''
PASSWORD = ''

# 将保存到文件的图像 URL。
URL_IMAGE = 'https://sandbox.oxylabs.io/assets/action-adventure.svg'

# Realtime URL。
API_URL = f'http://{USERNAME}:{PASSWORD}@realtime.oxylabs.io/v1/queries'


def dump_to_file(filename: str, data: bytes):
    with open(filename, 'wb') as file:
        file.write(data)


def main():
    parameters = {
        'source': 'universal',
        'url': URL_IMAGE,
        'content_encoding': 'base64',
    }
    response = requests.post(API_URL, json=parameters)
    if response.ok:
        data = json.loads(response.text)
        content_base64 = data['results'][0]['content']
        # 将 base64 编码的数据解码为字节。
        content = base64.b64decode(content_base64)
        dump_to_file('out.svg', content)


if __name__ == '__main__':
    main()

最后更新于

这有帮助吗?