赞
踩
因为百度图片是瀑布流, 所以我选择了用selenium模拟鼠标点击来下载图片
from selenium import webdriver import bs4 import time import urllib.request import re name = input('请输入你要搜索的名字:') number = int(input('请输入你要保存的文件数量:')) driver = webdriver.Chrome() driver.get('http://image.baidu.com/') #打开百度图片 time.sleep(2) driver.find_element_by_id('kw').send_keys(name) #搜索林依晨 driver.find_element_by_class_name('s_search').click() m = 0 for i in range(5): #划5下 js = "window.scrollTo(0,document.body.scrollHeight)" driver.execute_script(js) #滚到最底部 time.sleep(1) soup = bs4.BeautifulSoup(driver.page_source, 'lxml') imgsrc = soup.body.find_all('img', class_="main_img img-hover") for img in imgsrc: images = img.attrs['src'] urllib.request.urlretrieve(images, r'E:\python\爬的图片\%d.jpg' % m) m += 1 time.sleep(1) if m > number: break print('finish...')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。