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实现博客文章爬虫示例

复制代码 代码如下:#!/usr/bin/python#-*-coding:utf-8-*-# JCrawler# Author: Jam <810441377@qq.com>...

Python爬取腾讯视频评论的思路详解

Python爬取腾讯视频评论的思路详解

一、前提条件 安装了Fiddler了(用于抓包分析) 谷歌或火狐浏览器 如果是谷歌浏览器,还需要给谷歌浏览器安装一个SwitchyOmega插件,用于代理服务器 有Py...

python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程

实例如下所示: import requests import re,sys,os import json import threading import pprint class s...

Python爬虫爬验证码实现功能详解

Python爬虫爬验证码实现功能详解

主要实现功能: - 登陆网页 - 动态等待网页加载 - 验证码下载 很早就有一个想法,就是自动按照脚本执行一个功能,节省大量的人力——个人比较懒。花了几天写了写,本着想完成验证码的...

浅谈Python爬虫基本套路

浅谈Python爬虫基本套路

什么是爬虫? 网络爬虫也叫网络蜘蛛,如果把互联网比喻成一个蜘蛛网,那么蜘蛛就是在网上爬来爬去的蜘蛛,爬虫程序通过请求url地址,根据响应的内容进行解析采集数据, 比如:如果响应内容是ht...