账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    scrapy 如何下载图片并分类?
    37
    0

    问题描述

    在下载http://www.umei.cc/p/gaoqing/...,无法将一个图集放到同一个目录中

    问题出现的环境背景及自己尝试过哪些方法

    尝试了网上很多方法,无法解决

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)

    #coding:utf-8
    import random
    import re
    import urllib2
    from urllib import urlopen
    
    import requests
    import logging
    
    import time
    from bs4 import BeautifulSoup,Comment
    from scrapy.spiders import CrawlSpider, Rule
    from scrapy.linkextractors import LinkExtractor
    from z2.items import Z2Item
    from scrapy.http import Request
    
    logging.basicConfig(
        level=logging.INFO,
        format=
        '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
        datefmt='%a, %d %b %Y %H:%M:%S',
        filename='cataline.log',
        filemode='w')
    
    class Spider(CrawlSpider):
        name = 'z2'
        img_urls = []
        allowed_domains = ["www.umei.cc"]
        start_urls = ['http://www.umei.cc/p/gaoqing/rihan/']
        # rules = (
        #     Rule(LinkExtractor(allow=('http://www.umei.cc/p/gaoqing/rihan/\d{1,6}.htm',), deny=('http://www.umei.cc/p/gaoqing/rihan/\d{1,6}_\d{1,6}.htm')),
        #          callback='parse_z2_info', follow=True),
        # )
    
        def start_requests(self):
            yield Request(url='http://www.umei.cc/p/gaoqing/rihan/',
                          callback=self.parse_z2_key)
    
        def parse_z2_key(self, response):
            soup = BeautifulSoup(response.body, "lxml")
            content = soup.find("div", attrs={'class': 'TypeList'})
            # logging.debug(content)
            for link in content.findAll("a", attrs={'href': re.compile( r'(.*)(/rihan/)(\d{1,6})(.htm)'), 'class': 'TypeBigPics'}):
                logging.debug(link['href'])
                yield Request(url=link['href'],
                              callback=self.parse_z2_info)
                break
    
        def parse_z2_info(self, response):
            soup = BeautifulSoup(response.body, "lxml")
            item = Z2Item()
            # 去除html注释
            for element in soup(text=lambda text: isinstance(text, Comment)):
                element.extract()
    
            # 过滤script标签
            [s.extract() for s in soup('script')]
    
            # 过滤b标签
            [s.extract() for s in soup('b')]
    
    
            ArticleDesc = soup.find("p", attrs={'class': 'ArticleDesc'})
            logging.debug(ArticleDesc.get_text())
    
            Pages = soup.find("div", attrs={'class': 'NewPages'}).find('li')
            pageCounts = filter(str.isdigit, Pages.get_text().encode('gbk'))
            # 第一种含中文的字符串中提取数字的方法
            # logging.debug(re.findall(r"\d+\.?\d*", Pages.get_text())[0])
    
            # 第二种
            # logging.debug(Pages.get_text()[1:-3])
    
            # 第三种
            logging.debug(filter(str.isdigit, Pages.get_text().encode('gbk')))
    
            # img = soup.find("div", attrs={'class': 'ImageBody'}).find('img')
            # url = img.attrs['src']
            # self.img_urls.append(url)
            # logging.debug(self.img_urls)
    
            item['name'] = re.match(".*/(\d+)", response.url).group(1)
            logging.debug(item['name'])
    
            # image_urls = []
            # item['image_urls'] = image_urls
            sourceUrl = response.url[0:-4]
            # logging.debug(sourceUrl)
            for i in xrange(1, int(pageCounts) + 1):
                nextUrl = sourceUrl + '_' + str(i) + '.htm'
                # logging.debug(nextUrl)
                yield  Request(url=nextUrl,callback=self.parse_z2_single_img)
            item['image_urls'] = self.img_urls
            yield item
    
    
        def parse_z2_single_img(self, response):
            soup = BeautifulSoup(response.body, "lxml")
            img = soup.find("div", attrs={'class': 'ImageBody'}).find('img')
            url = img.attrs['src']
            self.img_urls.append(url)
    
    
    
    
    
    
    

    你期待的结果是什么?实际看到的错误信息又是什么?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 十月二十七 普通会员 1楼

      Scrapy 是一个用于爬取和分析数据的 Python 框架。在 Scrapy 中,可以使用 Image 模块来下载图片并对其进行分类。以下是一个简单的例子:

      首先,确保已经安装了 requestsBeautifulSoup 模块。如果没有,请运行以下命令安装:

      bash pip install requests beautifulsoup4

      接下来,创建一个 Scrapy 应用并添加一个 Scrapy爬虫 项目:

      bash scrapy startproject image_recognition cd image_recognition

      然后,在项目中创建一个 settings.py 文件,以便指定爬虫的网络配置:

      ```python import scrapy

      class ImageRecognitionSpider(scrapy.Spider): name = 'image_recognition' allowed_domains = ['example.com'] start_urls = ['http://example.com']

      def parse(self, response):
          for image in response.css('img'):
              yield {
                  'image_url': image['src'],
                  'category': image.css('h1::text').get(),
              }
      

      ```

      在这个例子中,http://example.com 是网站的 URL,而 h1::text 是一个 CSS 样式选择器,用于获取页面上的所有标题文本。这将返回一个字典,包含每个图片的 URL 和它的分类。

      要运行这个爬虫,打开终端或命令提示符,并导航到项目的目录。然后运行以下命令:

      bash scrapy crawl image_recognition

      这将启动 Scrapy 爬虫,并开始抓取指定网站上的所有图片。爬虫将使用 Image 模块下载图片,并使用 CSS 样式选择器来分类图片。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部