下载图片

可以通过 网络爬虫 API 下载图片。如果您通过代理端口下载图片, 则可以简单地将输出保存为图像扩展。例如:

curl -k -x realtime.oxylabs.io:60000 -U user:pass1 "https://example.com/image.jpg" >> image.jpg

如果您正在使用 推拉Realtime 集成方法,则需要添加 content_encoding 参数,其值为 base64。在收到结果后,您需要将content的编码数据解码成字节,并将其保存为图像文件。

Python 的示例如下。

import base64
import json
import requests

# Your credentials.
USERNAME = ''
PASSWORD = ''

# Image URL which will be saved to file.
URL_IMAGE = 'https://example.com/image.jpg'

# 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']
        # Decode base64 encoded data into bytes.
        content = base64.b64decode(content_base64)
        dump_to_file('out.jpg', content)


if __name__ == '__main__':
    main()

Last updated