zoukankan      html  css  js  c++  java
  • python爬取百度图片后自动上传

    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=&copyright=&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("风景"))

  • 相关阅读:
    Dart中的类型转换总结:
    【Dart学习】--Dart之数组(List)的相关方法总结
    Navigator的使用:
    001——Angular环境搭建、运行项目、搭建项目
    Dart中的数据类型转换:
    Flutter中的Stack、Align、Positioned的使用
    Flutter设置图片为正方形
    顶部导航TabBar、TabBarView、DefaultTabController
    《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <二> NSMutableString
    Swift随记
  • 原文地址:https://www.cnblogs.com/xiamaojjie/p/13921613.html
Copyright © 2011-2022 走看看