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

  • 相关阅读:
    POJ3481(待完善版本,请看注释)
    Heap_Sort金老师模板
    poj2255(Tree_Recover)
    快慢指针
    Link_List
    Perl_Tkx_Canvas绘图功能函数介绍
    配置管理
    变更管理
    合同管理
    收尾存在的问题
  • 原文地址:https://www.cnblogs.com/xiamaojjie/p/13921613.html
Copyright © 2011-2022 走看看