import requests def upload_image(imagePath): url = "https://test-oss.gamesegirl.com/oss-web/new_upload-image" files = { "file": ("蓝天白云.jpg", open(r"{}".format(imagePath), "rb"), "image/jpeg") } response = requests.post(url=url, files=files).json() print(response['content']['url']) if response['status'] == "OK" else print("图片上传失败") upload_image(r'C:UsersepalPicturesCamera Roll第107张.jpg')
爬取百度图片自动上传
from requests_html import HTMLSession import random import sys import requests def spider_image(search_content): session = HTMLSession() img_url_list = [] url = f'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={search_content}' f'&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=©right=&word={search_content}' f'&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&rn=30&gsm=' '&pn=30000' pn = 0 res = session.get(f'{url}&pn={pn}') if res.json()['bdIsClustered'] == '2': pass else: pn += 30 for dic in res.json()['data']: img_url = dic.get('thumbURL') if img_url: img_url_list.append(img_url) mun = 0 for url in random.sample(img_url_list, 1): mun += 1 # 访问图片链接 response = session.get(url) # 保存二进制并保存至本地 current_path = sys.argv[0] image_path = f"{current_path}第{mun}张.jpg" with open(f"{current_path}第{mun}张.jpg", 'wb') as fw: fw.write(response.content) # print(f'第{mun}张保存本地完毕') return image_path def upload_image(imagePath): url = "https://test-oss.gamesegirl.com/oss-web/new_upload-image" files = { "file": ("蓝天白云.jpg", open(r"{}".format(imagePath), "rb"), "image/jpeg") } response = requests.post(url=url, files=files).json() print(response['content']['url']) if response['status'] == "OK" else print("图片上传失败") upload_image(spider_image("风景"))