python 爬取图片 封装代码

import requests
import re, os
from lxml import etree
from urllib import request
import json

MOGU_PROXY_URL = ‘http://piping.mogumiao.com/proxy/api/get_ip_bs?appKey=fc1a46b572d54ca0a12f375eceb3b5e8&count=20&expiryDate=0&format=1&newLine=2
FREE_PROXY_URL = ‘http://192.168.221.221:5010/get/
TIANTANG_INDEX_URL = ‘http://www.ivsky.com/
GET_PROXY_TIMEOUT = 2

def get_mogu_proxies():
“”"
请求付费代理
:return:
“”"
try:
resp = requests.get(MOGU_PROXY_URL)
except Exception as e:
print(“获取代理失败”, e, resp.status_code)
if resp.status_code == 200:
resp_dict = json.loads(resp.text)
raw_proxies = resp_dict[‘msg’]
# 组装为requests包代理参数需要的格式
res_proxies = []
for proxy in raw_proxies:
proxy_type = ‘https’
proxy_url = ‘https://’ + proxy[‘ip’] + proxy[‘port’]
res_proxies.append({proxy_type, proxy_url})
return res_proxies

class IvskySpider(object):

def __init__(self):
    self.url = 'http://www.ivsky.com/tupian/ziranfengguang/index_2.html'
    # self.html = ''
    self.title = ''
    self.count = 0
    self.headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'
    }
    # 创建文件夹
    self.create_directry()

# 请求源代码
def get_html(self, url):
    response = requests.get(url=url, headers=self.headers)
    self.html = etree.HTML(response.text)

# 创建文件夹
def create_directry(self):
    # 获取源代码提取标题
    self.get_html(self.url)
    pass

# 解析网页
def parse_html(self):
    # 详情页
    pattern ='//ul/li/div[@class="il_img"]/a/@href'
    pattern2 = '//ul/li/div[@class="il_img"]/a/@title'
    html_content_name = self.html.xpath(pattern2)
    for name in html_content_name:
        self.title = name
        if not os.path.exists(self.title):
            os.mkdir(name)
    # 详情页链接
    html_content_url = self.html.xpath(pattern)
    for url in html_content_url:
        self.url_get = 'http://www.ivsky.com' + url
        # print(url_get)
        self.html_content(pattern2)

# 解析详情页的图片
def html_content(self,path):
    # print(self.url_get)
    response = requests.get(url=self.url_get, headers=self.headers)
    self.html = etree.HTML(response.text)
    picture = self.html.xpath('//ul/li/div[@class="il_img"]//img/@src')

    for pic in picture:

        # pic = request.get(url=pic)
        self.count += 1
        print('正在下载第%s张图片,请稍后。。。。' % self.count)
        # img_name = pic.split('/')[-1]
        path = self.title + '/' + '%s.jpg' % self.count
        # with open(path+'/'+self.count+'.jpg', 'wb') as f:
        #     f.write(pic)
        #     f.fileno()
        # print(pic)
        request.urlretrieve(pic, path)

# 下一页
def index_html(self):
    # 下一页
    page = '//div[@class="pagelist"]/a[@class="page-next"]/@href'
    page_next = self.html.xpath(page)
    for p_next in page_next:
        self.url = 'http://www.ivsky.com' + p_next
        print(self.url)

def run(self):
    for x in range(9):
        print('-'*12)
        self.get_html(url=self.url)
        self.index_html()
        self.parse_html()

if name == ‘main’:
ivsky = IvskySpider()
ivsky.run()
这是爬取的天堂网图片,使用一些包,requests包、os包、获取网页中的图片信息,然后爬取下来。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值