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爬虫自动创建文件夹的功能

该爬虫应用了创建文件夹的功能: #file setting folder_path = "D:/spider_things/2016.4.6/" + file_name +"/" i...

python爬虫模拟浏览器的两种方法实例分析

python爬虫模拟浏览器的两种方法实例分析

本文实例讲述了python爬虫模拟浏览器的两种方法。分享给大家供大家参考,具体如下: 爬虫爬取网站出现403,因为站点做了防爬虫的设置 一、Herders 属性 爬取CSDN博客 i...

Python抓取电影天堂电影信息的代码

Python2.7Mac OS 抓取的是电影天堂里面最新电影的页面。链接地址: http://www.dytt8.net/html/gndy/dyzz/index.html 获取页面的中...

python3 实现爬取TOP500的音乐信息并存储到mongoDB数据库中

python3 实现爬取TOP500的音乐信息并存储到mongoDB数据库中

爬取TOP500的音乐信息,包括排名情况、歌曲名、歌曲时间。 网页版酷狗不能手动翻页进行下一步的浏览,仔细观察第一页的URL: http://www.kugou.com/yy/rank/...

python实现爬虫统计学校BBS男女比例(一)

python实现爬虫统计学校BBS男女比例(一)

一、项目需求 前言:BBS上每个id对应一个用户,他们注册时候会填写性别(男、女、保密三选一)。 经过检查,BBS注册用户的id对应1-300000,大概是30万的用户 笔者想用Pyth...