赞
踩
- # -- coding: utf-8 --
- import re
- import time
-
- import requests
- from selenium import webdriver
- import os
-
- if not os.path.exists('imgs'):
- os.mkdir('imgs')
-
- bd_browser = webdriver.Chrome('chromedriver.exe')
- bd_browser.maximize_window()
- with open('stealth.min.js', 'r', encoding='utf-8')as f:
- js_code = f.read()
-
-
- bd_browser.get('https://image.baidu.com/')
- input_jd = bd_browser.find_element_by_id('kw')
- input_jd.send_keys(input("请输入要搜索图片的名字:"))
-
- cick_job = bd_browser.find_element_by_class_name('s_newBtn')
- cick_job.click()
- bd_browser.implicitly_wait(20)
- def drop_down():
- for i in range(100):
- time.sleep(0.3)
- bd_browser.execute_script('window.scrollBy(0,300)', '')
- bd_browser.implicitly_wait(50)
-
- def Download():
- lis = bd_browser.find_elements_by_class_name('main_img')
- titles = bd_browser.find_elements_by_class_name('imgitem-title')
- names = []
- urls = []
- for name in titles:
- name = name.get_attribute('title')
- name2 = name.replace(" ", "").replace("|", "").replace("?", "").replace(":", "").replace("/", "").replace(">", "").replace(".", "").replace(",", "").replace("_", "")
- name3 = re.sub('"', "", name2)
- names.append(name3)
-
- for url in lis:
- url = url.get_attribute('src')
- urls.append(url)
- headers = {
- "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
- }
-
-
- result = dict(zip(names, urls))
- for k, v in result.items():
- img = requests.get(v, headers=headers).content
- try:
- with open('imgs/'+k+".png", 'wb') as f:
- f.write(img)
- except:
- print(k+"不符合古命名规则")
-
- if __name__ == '__main__':
- drop_down()
- Download()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。