<small id='hhq5m'></small><noframes id='hhq5m'>

    <legend id='hhq5m'><style id='hhq5m'><dir id='hhq5m'><q id='hhq5m'></q></dir></style></legend>
  1. <i id='hhq5m'><tr id='hhq5m'><dt id='hhq5m'><q id='hhq5m'><span id='hhq5m'><b id='hhq5m'><form id='hhq5m'><ins id='hhq5m'></ins><ul id='hhq5m'></ul><sub id='hhq5m'></sub></form><legend id='hhq5m'></legend><bdo id='hhq5m'><pre id='hhq5m'><center id='hhq5m'></center></pre></bdo></b><th id='hhq5m'></th></span></q></dt></tr></i><div id='hhq5m'><tfoot id='hhq5m'></tfoot><dl id='hhq5m'><fieldset id='hhq5m'></fieldset></dl></div>

      <tfoot id='hhq5m'></tfoot>
        <bdo id='hhq5m'></bdo><ul id='hhq5m'></ul>
    1. 使用scrapy ImagesPipeline爬取图片资源的示例代码

      时间:2023-12-16

        <legend id='eNw2I'><style id='eNw2I'><dir id='eNw2I'><q id='eNw2I'></q></dir></style></legend>
          • <tfoot id='eNw2I'></tfoot>
              <tbody id='eNw2I'></tbody>
            <i id='eNw2I'><tr id='eNw2I'><dt id='eNw2I'><q id='eNw2I'><span id='eNw2I'><b id='eNw2I'><form id='eNw2I'><ins id='eNw2I'></ins><ul id='eNw2I'></ul><sub id='eNw2I'></sub></form><legend id='eNw2I'></legend><bdo id='eNw2I'><pre id='eNw2I'><center id='eNw2I'></center></pre></bdo></b><th id='eNw2I'></th></span></q></dt></tr></i><div id='eNw2I'><tfoot id='eNw2I'></tfoot><dl id='eNw2I'><fieldset id='eNw2I'></fieldset></dl></div>
            • <bdo id='eNw2I'></bdo><ul id='eNw2I'></ul>

              1. <small id='eNw2I'></small><noframes id='eNw2I'>

                使用Scrapy内置的ImagesPipeline可以非常方便地爬取网页上的图片资源。下面是完整的攻略和示例代码:

                1. 在settings.py中设置ImagesPipeline

                首先需要在项目的settings.py文件中进行一些配置。具体如下:

                ITEM_PIPELINES = {
                    'scrapy.pipelines.images.ImagesPipeline': 1
                }
                
                IMAGES_STORE = '/path/to/your/images/directory'
                
                • ITEM_PIPELINES中添加'scrapy.pipelines.images.ImagesPipeline': 1,表示该请求需要经过ImagesPipeline处理
                • IMAGES_STORE设置图片存储的目录路径

                2. 在Spider中使用item传递图片链接

                在自己的Spider中,需要定义一个item,用于存储图片的链接地址和其他信息:

                import scrapy
                
                class MyItem(scrapy.Item):
                    image_urls = scrapy.Field()
                    images = scrapy.Field()
                

                image_urls字段存储的是图片的链接地址,images字段会在下载完成后自动生成,存储图片的本地路径。

                3. 在Spider中yield item并指定image_urls字段

                在Spider中,需要在解析网页的流程中yield item,并在item中指定image_urls字段:

                import scrapy
                
                from myproject.items import MyItem
                
                class MySpider(scrapy.Spider):
                    name = 'myspider'
                    start_urls = [
                        'http://www.example.com',
                        'http://www.example.com/page/2',
                        'http://www.example.com/page/3',
                    ]
                
                    def parse(self, response):
                        item = MyItem()
                        item['image_urls'] = response.css('img::attr("src")').getall()
                        yield item
                

                在这个示例中,我们从响应中获取所有图片的src属性,并存入item的image_urls中。

                4. 运行spider并查看运行效果

                在运行Spider之前,需要确保IMAGES_STORE设置的目录存在,并有写入权限。

                接下来就可以运行我们的Spider,Scrapy会自动从指定的链接下载图片资源,保存至IMAGES_STORE设置的目录中。

                可以在Spider运行时添加-v INFO参数,查看下载进度。

                scrapy crawl myspider -v INFO
                

                示例1:下载豆瓣读书封面图片

                下面是下载豆瓣读书封面图片的示例代码:

                import scrapy
                
                from myproject.items import MyItem
                
                class DoubanBooksSpider(scrapy.Spider):
                    name = 'doubanbooks'
                    start_urls = [
                        'https://book.douban.com/top250',
                    ]
                
                    def parse(self, response):
                        for book in response.css('tr.item'):
                            item = MyItem()
                            item['title'] = book.css('div.pl2 a::text').get()
                            item['image_urls'] = [book.css('img::attr(src)').get()]
                            yield item
                

                在这个示例中,我们可以获取豆瓣读书榜单的前250本图书的封面,保存在本地文件。

                示例2:下载糗事百科用户头像图片

                下面是下载糗事百科用户头像图片的示例代码:

                import scrapy
                
                from myproject.items import MyItem
                
                class QSBKSpider(scrapy.Spider):
                    name = 'qsbk'
                    start_urls = [
                        'https://www.qiushibaike.com/',
                    ]
                
                    def parse(self, response):
                        for user in response.css('div.article'):
                            item = MyItem()
                            item['username'] = user.css('div.header a::text').get()
                            item['user_url'] = user.css('div.header a::attr(href)').get()
                            item['image_urls'] = [user.css('img.avatar::attr(src)').get()]
                            yield item
                

                在这个示例中,我们可以获取糗事百科上的用户头像,并保存在本地文件。

                上一篇:Python读写锁实现实现代码解析 下一篇:Python OpenCV图像颜色变换示例

                相关文章

              2. <small id='7VgZ1'></small><noframes id='7VgZ1'>

                    <bdo id='7VgZ1'></bdo><ul id='7VgZ1'></ul>
                1. <tfoot id='7VgZ1'></tfoot>

                2. <i id='7VgZ1'><tr id='7VgZ1'><dt id='7VgZ1'><q id='7VgZ1'><span id='7VgZ1'><b id='7VgZ1'><form id='7VgZ1'><ins id='7VgZ1'></ins><ul id='7VgZ1'></ul><sub id='7VgZ1'></sub></form><legend id='7VgZ1'></legend><bdo id='7VgZ1'><pre id='7VgZ1'><center id='7VgZ1'></center></pre></bdo></b><th id='7VgZ1'></th></span></q></dt></tr></i><div id='7VgZ1'><tfoot id='7VgZ1'></tfoot><dl id='7VgZ1'><fieldset id='7VgZ1'></fieldset></dl></div>

                  1. <legend id='7VgZ1'><style id='7VgZ1'><dir id='7VgZ1'><q id='7VgZ1'></q></dir></style></legend>