python 上传图片搜索图片

  在搜索引擎上通过上传图片搜索图片

  

 

 

 

   通过抓包工具Fiddler获取相关信息

 

  1、上传图片文件

  

 

  

  

  上传图片链接

  

 

 

 

  测试代码

#在百度搜索引擎上传图片(上传文件),获取图片检索链接
    def uploadFile(self,fileName):
        # url='https://graph.baidu.com/upload?tn=pc&from=pc&image_source=PC_UPLOAD_IMAGE_FILE'
        url = 'https://graph.baidu.com/upload'
        header = {
            'Host': 'graph.baidu.com',
            'Referer': 'https://image.baidu.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
            'Origin': 'https://image.baidu.com',
            # 'Cookie':'PSTM=1649927841; BAIDUID=FA651255B03B80F9C21003ADB4677328:FG=1; BIDUPSID=5B8AAE4850EBC458993196C12AF00649; MCITY=-131%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BA_HECTOR=8la42g2g21a4a581ag24ak7p1hketp01b; ZFY=s0GdMV2bK5NpqnzKk7PNiSWtLQEMj9uOuJVVhvITDgI:C; BAIDUID_BFESS=FA651255B03B80F9C21003ADB4677328:FG=1; delPer=0; PSINO=1; BDSFRCVID=z5FOJexroG0G6HJjOpIgrFHUneKK0gOTDYrEOwXPsp3LGJLVc5lGEG0PtOpgko4-oxeCogKK0mOTHUuF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BDSFRCVID_BFESS=z5FOJexroG0G6HJjOpIgrFHUneKK0gOTDYrEOwXPsp3LGJLVc5lGEG0PtOpgko4-oxeCogKK0mOTHUuF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BCLID=10863385960832625789; BCLID_BFESS=10863385960832625789; H_BDCLCKID_SF=tRk8oI8KJKvDqTrP-trf5DCShUFsL6crB2Q-XPoO3KJnMln_Mxn2yj0XQ47P2tQiWbRM2Mbgy4op8P3y0bb2DUA1y4vp55QQ32TxoUJ2B-JJjnAmqtnWbftebPRiJPr9QgbP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AAAAAAAAAAAAAAAAAAAAAABeaR2MXmkdjM2; BDRCVFR[Txj84yDU4nc]=mk3SLVN4HKm; H_PS_PSSID=36552_37559_36921_37495_37300_36885_37403_37404_36789_37538_37497_26350_37343_37372; ab_sr=1.0.1_NzU2MzAyNGUyYWJiZjQ3Y2NkZjUxYWJhZDUzZjQ1M2Y4MTg5NGFkMmZiZmI1Y2Q3NTk5YjIxNDIzNWM1MWM5M2RiZWFhZTc0MTRiODRhMzkyMjdhNmJkNzdkNzQ3NTY3ZjA5YzRlMTEzZTcwYTczYjMzZTI3OGZmZjIzMmUwNDFmM2RhZTU3YTlhY2Y3N2YzMzU3ZTU1NjhjYjZhZmY4NQ==; antispam_data=4d293a7b27ad6faa1f130c9f597ecf402e1121cfc9d9772d24b0d757f06c3b39ec11c7f43e9c7f3598532e1c011d0388cf8bb92fcb0b3183fd86d8ff01cf501d1af249b7f60e52224e24a4be7d065ed6'
            # 'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
            # 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryV5zByL2DYh7VhAxR',
            # 'sec-ch-ua-mobile': '?0',
            # 'sec-ch-ua-platform': '"Windows"',
            # 'Sec-Fetch-Site': 'same-site',
            # 'Sec-Fetch-Mode': 'cors',
            # 'Sec-Fetch-Dest': 'empty',
            # 'Accept-Encoding': 'gzip, deflate, br',
            # 'Accept-Language': 'zh-CN,zh;q=0.9'
        }
        # fileName = '伊利.jpg'
        file_Name = parse.quote(os.path.basename(fileName))
        files = {
            # 'image': (fileName, open('D:/test/伊利.jpg', 'rb'), 'image/jpeg'),
            'image': (file_Name, open(fileName, 'rb'), 'image/jpeg'),
            'pos': 'upload',
            'uptype': 'upload_pc',
            'fm': 'index'
        }
        uptime = commonMethod.get_current_timestamp() * 1000
        post_data = {
            "extUiData[isLogoShow]": 1,
            'from': 'pc',
            'image_source': 'PC_UPLOAD_IMAGE_FILE',
            "range": "{\"page_from\": \"imageIndex\"}",
            'tn': 'pc',
            'sdkParams': '{"data":"4d293a7b27ad6faa1f130c9f597ecf402e1121cfc9d9772d24b0d757f06c3b39ec11c7f43e9c7f3598532e1c011d038843ae68a27690230fcafcb8a660447ba72869a723a3cd2e98c5dd4bf2a9034067","key_id":"23","sign":"3cb26a42"}',
            # 'uptime': 1665640116095
            'uptime': uptime
        }
        try:

            response = requests.post(url, headers=header, files=files, data=post_data, verify=False).text
            data_dict = json.loads(response)
            if data_dict != None:
                if 'msg' in data_dict and data_dict['msg'] == 'Success':
                    if 'data' in data_dict and data_dict['data'] != None:
                        url = data_dict['data']['url']
                        print(url)
                        sign = data_dict['data']['sign']
                        print(sign)


            return url
        except Exception as ex:
            print(ex)

    # 在百度搜索引擎上传图片(上传文件url),获取图片检索链接
    def uploadFileFromUrl(self,pictureurl):
        # https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111110&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E4%BC%8A%E5%88%A9
        # url='https://graph.baidu.com/upload?tn=pc&from=pc&image_source=PC_UPLOAD_IMAGE_FILE'
        url = 'https://graph.baidu.com/upload'
        header = {
            'Host': 'graph.baidu.com',
            'Referer': 'https://image.baidu.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
            'Origin': 'https://image.baidu.com',
            # 'Cookie':'PSTM=1649927841; BAIDUID=FA651255B03B80F9C21003ADB4677328:FG=1; BIDUPSID=5B8AAE4850EBC458993196C12AF00649; MCITY=-131%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BA_HECTOR=8la42g2g21a4a581ag24ak7p1hketp01b; ZFY=s0GdMV2bK5NpqnzKk7PNiSWtLQEMj9uOuJVVhvITDgI:C; BAIDUID_BFESS=FA651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63]=mk3SLVN4HKm; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDUSS=J0WXBNcFNVLTR-VHk1eDVFWlNCQUZxaEplZzdIRTZnfjgyenp0MHRCa1hKMjlqRUFBQUFBJCQAAAAAAAAAAAEAAAA0w~oGc2hhb19za3MAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABeaR2MXmkdjM2; BDUSS_BFESS=J0WXBNcFNVLTR-VHk1eDVFWlNCQUZxaEplZzdIRTZnfjgyenp0MHRCa1hKMjlqRUFBQUFBJCQAAAAAAAAAAAEAAAA0w~oGc2hhb19za3MAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABeaR2MXmkdjM2; BDRCVFR[Txj84yDU4nc]=mk3SLVN4HKm; H_PS_PSSID=36552_37559_36921_37495_37300_36885_37403_37404_36789_37538_37497_26350_37343_37372; ab_sr=1.0.1_NzU2MzAyNGUyYWJiZjQ3Y2NkZjUxYWJhZDUzZjQ1M2Y4MTg5NGFkMmZiZmI1Y2Q3NTk5YjIxNDIzNWM1MWM5M2RiZWFhZTc0MTRiODRhMzkyMjdhNmJkNzdkNzQ3NTY3ZjA5YzRlMTEzZTcwYTczYjMzZTI3OGZmZjIzMmUwNDFmM2RhZTU3YTlhY2Y3N2YzMzU3ZTU1NjhjYjZhZmY4NQ==; antispam_data=4d293a7b27ad6faa1f130c9f597ecf402e1121cfc9d9772d24b0d757f06c3b39ec11c7f43e9c7f3598532e1c011d0388cf8bb92fcb0b3183fd86d8ff01cf501d1af249b7f60e52224e24a4be7d065ed6'
            # 'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
            # 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryV5zByL2DYh7VhAxR',
            # 'sec-ch-ua-mobile': '?0',
            # 'sec-ch-ua-platform': '"Windows"',
            # 'Sec-Fetch-Site': 'same-site',
            # 'Sec-Fetch-Mode': 'cors',
            # 'Sec-Fetch-Dest': 'empty',
            # 'Accept-Encoding': 'gzip, deflate, br',
            # 'Accept-Language': 'zh-CN,zh;q=0.9'
        }
        uptime = commonMethod.get_current_timestamp() * 1000
        post_data = {
            "extUiData[isLogoShow]": 1,
            'from': 'pc',
            'image': pictureurl,
            'image_source': 'PC_UPLOAD_URL',
            "range": "{\"page_from\": \"imagePage\"}",
            'tn': 'pc',
            'sdkParams': '{"data":"4d293a7b27ad6faa1f130c9f597ecf402e1121cfc9d9772d24b0d757f06c3b39ec11c7f43e9c7f3598532e1c011d03884e2e8427baf26af7da99a99ee5e9aca6ecaf29ab19be6c83ea9cfbca72b4aa3f","key_id":"23","sign":"c1c26a26"}',
            # 'uptime': 1665640116095
            'uptime': uptime
        }
        try:
            response = requests.post(url, headers=header, data=post_data, verify=False).text
            data_dict = json.loads(response)
            if data_dict != None:
                if 'msg' in data_dict and data_dict['msg'] == 'Success':
                    if 'data' in data_dict and data_dict['data'] != None:
                        url = data_dict['data']['url']
                        print(url)
                        sign = data_dict['data']['sign']
                        print(sign)

            return url
        except Exception as ex:
            print(ex)

    def catchPictureFromBDSearch(self,url):
        header = {
            # 'Host': 'jvod.300hu.com',
            # 'Referer': product_url,
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
        }
        userAgent = commonMethod.get_userAgent()
        driver = commonMethod.get_driver('', userAgent, False)
        driver.get(url)
        time.sleep(1)
        selenium_html = driver.execute_script("return document.documentElement.outerHTML")
        doc = pq(selenium_html)
        print(doc)
View Code

 

posted on 2022-10-13 16:24  shaomine  阅读(820)  评论(0编辑  收藏  举报