1 from selenium import webdriver 2 import requests 3 driver = webdriver.Chrome() 4 #打开优酷 5 driver.get("http://www.youku.com") 6 #点开优酷片库 7 driver.find_element_by_xpath("//*[@id='m_2544']/div/ul/li[6]/span[1]/a").click() 8 #获取所有img元素 9 listImg = driver.find_elements_by_xpath("//div[@class='vaule_main']//li/div/div/img") 10 #取img的src值 11 listSrc = [] 12 for img in listImg: 13 src = img.get_attribute('src') 14 listSrc.append(src) 15 print(len(listImg)) 16 #下载图片 17 session = requests.session() 18 for index,value in enumerate(listSrc): 19 response = session.get(value) 20 with open(str(index)+'.png',mode='wb') as f: 21 f.write(response.content)