欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python爬取妹子图片2

程序员文章站 2022-05-07 21:13:03
#-*-coding:utf-8 import requests import lxml import os from bs4 import BeautifulSoup...
#-*-coding:utf-8
import requests
import lxml
import os
from bs4 import BeautifulSoup
class MEZI():
    def __init__(self,url,pageIndex):
        self.url=url+str(pageIndex)
        self.headers = {'User-Agent':'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}

    #得到一页的网页源码
    def get_one_page_html(self):
        re=requests.get(self.url,headers=self.headers)
        html=re.text
        return html

        #得到一个妹子的写真集的url
    def get_one_person_url(self):
        all_a=[]
        html=self.get_one_page_html()
        soup=BeautifulSoup(html,'lxml')
        all_p=soup.find('p',class_='postlist')
        all_span=all_p.find('ul').find_all('span')
        for span in all_span:
            if span.find('a'):
                all_a.append(span.find('a').get('href'))
        return all_a

    def download(self,filename):   #filename是你要存储的路径名
        x=1
        all_a=self.get_one_person_url()
        for a in all_a:   #这里只是一张照片的地址
            for i in range(1,30):    #限制每个人下载30张照片
                url=a+'/'+str(i)
                re=requests.get(url ,headers=self.headers)
                print u'正在下载图片:%s'%re.url
                html=re.text
                soup=BeautifulSoup(html,'lxml')
                img_url=soup.find('img').get('src')
                html_img = requests.get(img_url, headers=self.headers)
                img_name=str(x)+'.jpg'
                path=os.path.normpath(os.path.join(filename,img_name))    #这里是规范你的绝对路径,os.path.join(filename,img_name)是将目录名和文件名连接在一起
                with open(path,'wb') as f:   #开始下载
                    f.write(html_img.content)
                x=x+1




if __name__=="__main__":
    url='https://www.mzitu.com/page/'
    for i in range(1,30):
        app = MEZI(url, i)
        filename = 'g:\\picture2'
        app.download(filename)