傻瓜版代码,先修改储存目录和webdriver路径,再输入网址和图片数量就能用了。
from selenium import webdriver import time from lxml import etree import random from urllib import request import os def pic_url_get(num): for i in range(num): time.sleep(random.uniform(0,2)) content = driver.page_source html = etree.HTML(content) pic_url = html.xpath('/*;q=0.8,application/signed-exchange;v=b3;q=0.9')] request.install_opener(opener) name = os.path.split(pic_url)[-1] if '.' not in name: name = name + '.jpg' try: request.urlretrieve(pic_url, r'C:UsersmsiDesktopgoogle{0}'.format(name)) print(name + '下载完成!!!') except: request.urlretrieve(pic_url, r'C:UsersmsiDesktopgoogle{0}.jpg'.format(a)) print(str(a) + '下载完成!!!') a+=1 if __name__ =='__main__': if os.path.isdir(r'C:UsersmsiDesktopGoogle'): #修改成你想要存储到的地方 pass else: os.mkdir(r'C:UsersmsiDesktopGoogle') #修改成你想要存储到的地方 num = int(input('请输入你想要下载的图片数量(整数):')) pic_urls = [] url = input('请输入你想要下载的谷歌图片网址:') driver_path = r'G:Pythonchromedriver' #修改成你的chromedriver的路径 driver = webdriver.Chrome(executable_path=driver_path) driver.get(url) first_pic_button = driver.find_element_by_xpath('//div[@id="islrg"]/div[1]/div[1]/a[1]') first_pic_button.click() pic_url_get(num) print(pic_urls) pics_download() print('-----------------------------------爬取完成------------------------------------------')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)