图片搜索引擎——python编写

Python图片搜索引擎

话不多说,直接上代码!

import requests
import re

headers = {
     'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.41"}

def GetPic(pic,path):
    url = "https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%C3%A8&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111"
    kw = {
     'word':pic}
    websites = re.findall('"objURL":"(.*?)"',requests.get(url,headers=headers,params=kw).content.decode('utf8'),re.DOTALL)
    i=0
    for website in websites:
        try:
            content = requests.get(website,headers=headers).content
            if website[-3:]=='jpg':
                with open(path+"/"+pic+"{}.jpg".format(i),'wb') as f:
                    f.write(content)
            elif website[-4:]=='jepg':
                with open(path+"/"+pic+"{}.jpeg".format(i),'wb') as f:
                    f.write(content)
            elif website[-3:]=='png':
                with open(path+"/"+pic+"{}.png".format(i),'wb') as f:
                    f.write(content)
            elif website[-3:]=='bmp':
                with open(path+"/"+pic+"{}.bmp".format(i),'wb') as f:
                    f.write(content)
            else:continue
        except:continue
        i+=1

if __name__ == '__main__':
    pic = input("请输入您想要搜索的图片的名称:")
    path = input("请输入您即将保存的路径:")
    GetPic(pic,path)

运行结果:
图片搜索引擎——python编写_第1张图片

你可能感兴趣的:(搜索引擎,python库,Python爬虫,搜索引擎,python,人工智能)