天天看點

練習爬取高清美女

from lxml import etree
import requests
import os
# 爬取彼岸圖網美女圖檔前三頁
if __name__ == '__main__':
    # 爬取到頁面源碼資料
    url = ['http://pic.netbian.com/4kmeinv/','http://pic.netbian.com/4kmeinv/index_2.html', 'http://pic.netbian.com/4kmeinv/index_3.html']
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63'
    }

    # 建立一個檔案夾
    if not os.path.exists('./picture'):
        os.mkdir('./picture')
    for i in url:
        response = requests.get(url=i, headers=headers)
        page_text = response.text
        # 資料解析
        tree = etree.HTML(page_text)
        li_list = tree.xpath('//div[@class="slist"]/ul/li')
        for li in li_list:
            img_href= 'http://pic.netbian.com' + li.xpath('./a//@href')[0]
            # 通用的解決中文亂碼的解決方案
            img_detail = requests.get(url=img_href, headers=headers).text
            detail_tree = etree.HTML(img_detail)
            img_detail_url = 'http://pic.netbian.com' + detail_tree.xpath('//div[@class="photo-pic"]/a/img/@src')[0]
            img_detail_data = requests.get(url=img_detail_url, headers=headers).content
            img_name = li.xpath('./a/img/@alt')[0]+'.jpg'
            img_name = img_name.encode('iso-8859-1').decode('gbk')
            img_path = 'picture/' + img_name
            with open(img_path, 'wb') as fp:
                fp.write(img_detail_data)
                print(img_name, "下載下傳成功")


           

供學習參考