Python使用Scrapy爬取妹子图

yipeiwu_com6年前Python爬虫

Python Scrapy爬虫,听说妹子图挺火,我整站爬取了,上周一共搞了大概8000多张图片。和大家分享一下。

核心爬虫代码

# -*- coding: utf-8 -*-
from scrapy.selector import Selector
import scrapy
from scrapy.contrib.loader import ItemLoader, Identity
from fun.items import MeizituItem
 
 
class MeizituSpider(scrapy.Spider):
  name = "meizitu"
  allowed_domains = ["meizitu.com"]
  start_urls = (
    'http://www.meizitu.com/',
  )
 
  def parse(self, response):
    sel = Selector(response)
    for link in sel.xpath('//h2/a/@href').extract():
      request = scrapy.Request(link, callback=self.parse_item)
      yield request
 
    pages = sel.xpath("//div[@class='navigation']/div[@id='wp_page_numbers']/ul/li/a/@href").extract()
    print('pages: %s' % pages)
    if len(pages) > 2:
      page_link = pages[-2]
      page_link = page_link.replace('/a/', '')  
      request = scrapy.Request('http://www.meizitu.com/a/%s' % page_link, callback=self.parse)
      yield request
 
  def parse_item(self, response):
    l = ItemLoader(item=MeizituItem(), response=response)
    l.add_xpath('name', '//h2/a/text()')
    l.add_xpath('tags', "//div[@id='maincontent']/div[@class='postmeta clearfix']/div[@class='metaRight']/p")
    l.add_xpath('image_urls', "//div[@id='picture']/p/img/@src", Identity())
 
    l.add_value('url', response.url)
    return l.load_item()

项目地址:https://github.com/ZhangBohan/fun_crawler

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

详解Python解决抓取内容乱码问题(decode和encode解码)

详解Python解决抓取内容乱码问题(decode和encode解码)

一、乱码问题描述 经常在爬虫或者一些操作的时候,经常会出现中文乱码等问题,如下 原因是源网页编码和爬取下来后的编码格式不一致  二、利用encode与decode解决乱码问题...

Python学习笔记之抓取某只基金历史净值数据实战案例

Python学习笔记之抓取某只基金历史净值数据实战案例

本文实例讲述了Python抓取某只基金历史净值数据。分享给大家供大家参考,具体如下: http://fund.eastmoney.com/f10/jjjz_519961.html 1、...

使用Python的Scrapy框架编写web爬虫的简单示例

 在这个教材中,我们假定你已经安装了Scrapy。假如你没有安装,你可以参考这个安装指南。 我们将会用开放目录项目(dmoz)作为我们例子去抓取。 这个教材将会带你走过下面这几...

python书籍信息爬虫实例

python书籍信息爬虫示例,供大家参考,具体内容如下 背景说明 需要收集一些书籍信息,以豆瓣书籍条目作为源,得到一些有效书籍信息,并保存到本地数据库。 获取书籍分类标签 具体可参考这个...

Python爬虫之UserAgent的使用实例

问题: 在Python爬虫的过程中经常要模拟UserAgent, 因此自动生成UserAgent十分有用, 最近看到一个Python库(fake-useragent),可以随机生成各种U...