爬取妹子图,几行代码拿到福利图

#-*- coding:utf-8 -*-
from selenium import webdriver
import time
import requests


class Meizi:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKi"

                          "t/537.36   (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}


    def parse_url(self,url):

        self.driver.get(url)


    def parse_content(self):
        img_url_list = self.driver.find_elements_by_xpath('//div[@class="pic"]/a')

        return img_url_list


    def t_pic(self,img_url_list):
        img_list = []
        for img_url in img_url_list:
            img = img_url.get_attribute('href')
            img_list.append(img)

        return img_list


    def save_img(self,img_list):
        for img in img_list:
            self.driver.get(img)
            a_list = self.driver.find_elements_by_xpath('//div[@id="picture"]/p/img')
            for a in a_list:
                b = a.get_attribute('src')
                filename = b.split('uploads/')[-1].replace('/', '_')
                r = requests.get(b, headers=self.headers)
                with open(filename, 'wb') as f:
                    f.write(r.content)
                print(b)


    def run(self):
        url = 'http://www.meizitu.com/a/more_1.html'
        self.parse_url(url)
        img_url_list = self.parse_content()
        img_list = self.t_pic(img_url_list)
        self.save_img(img_list)
        time.sleep(2)
        self.driver.quit()


if __name__ == '__main__':
    meizi = Meizi()
    meizi.run()

你可能感兴趣的:(python爬虫,爬虫练习,妹子图爬虫,selenium运用,python爬虫)