python 使用requests爬虫爬取图片

import os

import requests
from lxml import etree

class Img():
    def __init__(self):
        self.headers = {
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
        }
        self.start_url = "http://www.netbian.com/"

    def get_response(self, url):
        res = requests.get(url, headers = self.headers)
        return res.content

    def trans_res_html(self, res):
        html = etree.HTML(res)
        return html

    def run(self):
        if not os.path.exists("tupian"):
            # 创建文件夹
            os.mkdir("tupian")
        res = self.get_response(self.start_url)
        html = self.trans_res_html(res)

        list = html.xpath('//div[@id="main"]/div[@class="list"]/ul/li/a')
        #遍历
        for dz in list:
            item = {}
            item["a_href"] = "http://www.netbian.com/" + dz.xpath('./@href')[0]
            res2 = self.get_response(item["a_href"])
            html2 = self.trans_res_html(res2)
            src = html2.xpath('//div[@id="main"]//div[@class="pic"]/p/a/img/@src')[0]
            name = html2.xpath('//div[@id="main"]//div[@class="pic"]/p/a/img/@alt')[0]

            img_res = self.get_response(src)
            # 保存文件
            f = open("tupian/%s.jpg" % name, "wb")
            f.write(img_res)
            # 关闭
            f.close()
            print(item)

if __name__ == '__main__':
    img = Img()
    img.run()

你可能感兴趣的:(爬虫scripy,python)