import os, requests

登陆信息

username = 'srtzimu' password = '7018dwdx' cookie_file = 'cookie.txt'

目录路径

dir_path = r'C:\Users\Administrator\桌面\fsdownload' session = requests.session() login_url = 'https://imagetwist.com/login' response = session.get(login_url) login_data = { 'username': username, 'password': password, 'login': 'submit' } response = session.post(login_url, data=login_data) if response.status_code != 200: print('登陆成功!') exit() with open(cookie_file, 'w') as f: f.write('; '.join([f'{k}={v}' for k, v in session.cookies.items()]))

读取cookie

with open(cookie_file, 'r') as f: cookie_str = f.read().strip()

上传图片

upload_url = 'https://imagetwist.com/upload' for filename in os.listdir(dir_path): if filename.endswith('.jpg') or filename.endswith('.png'): filepath = os.path.join(dir_path, filename) print(f'Uploading {filepath}...') files = {'file1': (filename, open(filepath, 'rb'), 'image/jpeg')}

    # 设置请求头
    headers = {
        'Cookie': cookie_str,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    }
    payload = {
        'upload_type': 'file',
        'sess_id': 'qysa23rf7bfqme45'
    }
    response = requests.post(upload_url, files=files, headers=headers, data=payload)
    if response.status_code != 200:
        print(f'失败 {filepath}!')
    else:
        print(f'{response.url}')

测试返回url页面内容

test_url = response.url test_response = requests.get(test_url) if test_response.status_code != 200: print('返回url页面内容测试失败!') else: print('返回url页面内容测试成功!') print(test_response.text)

Python 图片上传并测试返回URL页面内容

原文地址: https://www.cveoy.top/t/topic/osgC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录