Giter VIP home page Giter VIP logo

igscraperkit's Introduction

IGScraperKit

Create dynamic web scraper in Objective-C or Ruby.

Usage

Create a scraper:

#import "IGScraperKit.h"
IGScraper* scraper = [IGScraper scraperWithBlock:^id(IGXMLNode* node, NSString* url) {
                return [[[node queryWithXPath:@"//p"] firstObject] text];
            }];

Then scrape HTML with scraper:

[scraper scrape:@"<html><p>Hello World</p></html>" URL:nil];
// => @"Hello World"
#import "IGScraperKit.h"

IGScraperRecipe* recipe = [[IGScraperRecipe alloc] init];
[recipe addURLPattern:[NSRegularExpression regularExpressionWithPattern:@"https://www\.google\.com/search\?q=.+" options:0 error:nil]
     withScraperBlock:^id(IGXMLNode *node, NSString *url) {
  // handling for the page ...
  return data;
}];
[recipe addURLPattern:[NSRegularExpression regularExpressionWithPattern:@"https://www\.google\.com/" options:0 error:nil]
  withScraperBlock:^id(IGXMLNode *node, NSString *url) {
  // handling for the page ...
  return data;
}];
[recipe scrapeWithHTML:html URL:URL];

Write Scraper With Ruby

If you want something more dynamic, you can define a Recipe in Ruby:

# A recipe scrape page based on URL
class GoogleRecipe < ScraperKit::Recipe
  title "Google Search"

  # define a HTML scraper by `on url`, where url can be a string or a Regexp
  on %r{https://www\.google\.com/search\?q=.+} do
    # doc is a HTMLDoc object represent the document
    doc.xpath('//h3/a').collect {|node| node.text }
  end

  # if the page you need to parse is not HTML
  # use `on_text url`
  on_text %r{https://www\.google\.com/search\.json.+} do
    # doc is a string of the document
    JSON.parse(doc)
  end
end

Then load the recipe into IGRecipeRegistry and parse the page:

#import "IGScraperKit.h"

// load the recipe
NSString* path = [[NSBundle mainBundle] pathForResource:@"google" ofType:@"rb"];
NSString* recipe = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
IGRecipeRegistry* registry = [[IGRecipeRegistry alloc] init];
[registry loadRecipe:Recipe(@"walmart")];

NSArray* result = [registry scrapeWithHTML:html url:@"https://www.google.com/search?q=doughnuts"];
// => <__NSArrayM 0xed85590>(
// Doughnut - Wikipedia, the free encyclopedia,
// Home - Krispy Kreme Doughnuts and Coffee,
// Voodoo Doughnut - The Magic is in the Hole!!!,
//【超人氣甜甜圈】Krispy Kreme Doughnuts 、台北店 ... - Yam天空部落,
// Doughnut Recipes - Allrecipes.com,
// Doughnut Plant,
// Revolution Doughnuts,
// Sidecar Doughnuts & Coffee,
// Top Pot Hand-Forged Doughnuts,
// Lucky's Doughnuts
// )

To use this, you will need to include JavaScriptCore framework (iOS 7, OS X 10.9) and define IGSCRAPERKIT_ENABLE_SCRIPTING before import IGScraperKit.h.

Installation

To install IGScraperKit throught CocoaPods, add following lines to your Podfile:

pod "IGScraperKit", '0.3.1'

Or with Ruby supports:

pod "IGScraperKit/Scripting", '0.3.1'

Dependencies

  • IGScraperKit use IGHTMLQuery for HTML processing. Check the ruby wrappers if you need to use the Ruby interface.
  • IGScraperKit use JavaScriptCore in iOS 7 and Opal for JavaScript support.
  • Use phantomjs to run Opal tests.

Development

  1. Install gems: In the project folder, run the command: bundle install
  2. Install cocoapods: Run the command: pod install

License

MIT License. Check LICENSE.txt.

igscraperkit's People

Contributors

siuying avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.