版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xcd1997/article/details/82526990
from selenium import webdriver import requests from selenium.webdriver import ActionChains import time def get_img(url,finame):#保存图片 # url = 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=271066229,2382325557&fm=27&gp=0.jpg' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36' } data = requests.get(url, headers=headers) with open(finame, 'wb') as f: f.write(data.content) def skip(d):#跳转到打开界面 d.switch_to.window(d.window_handles[-1]) return d def prepare(url):#准备事项 d = webdriver.Chrome() d.implicitly_wait(10) d.get(url) return d def run(): url = 'https://www.baidu.com' d = prepare(url) text = d.find_element_by_xpath('//*[@id="kw"]') buten1 = d.find_element_by_xpath('//*[@id="su"]') text.send_keys('刘诗诗龙葵图片') buten1.click() d = skip(d) data = d.find_element_by_xpath('//*[@id="1"]/h3/a') ActionChains(d).move_to_element(data).click().perform() d=skip(d) fs = d.find_element_by_xpath('//*[@id="imgid"]/div/ul/li[1]/div/a/img') ActionChains(d).move_to_element(fs).click().perform() d = skip(d) for i in range(20): img_url = d.find_element_by_xpath('//*[@id="currentImg"]').get_attribute('src') get_img(img_url,'D:\\龙葵\\'+str(i)+'.jpg') last = d.find_element_by_xpath('//*[@id="container"]/span[2]/span') time.sleep(1) last.click() time.sleep(3) d.close() d.quit() if __name__ == '__main__': run()