In this article, we will learn how to use Objective-C and the AFNetworking and Ono libraries to download all the images from a Wikipedia page.
—-
Overview
The goal is to extract the names, breed groups, local names, and image URLs for all dog breeds listed on this Wikipedia page. We will store the image URLs, download the images and save them to a local folder.
Here are the key steps we will cover:
- Import required frameworks
- Send HTTP request to fetch the Wikipedia page
- Parse the page HTML using Ono
- Find the table with dog breed data using a CSS selector
- Iterate through the table rows
- Extract data from each column
- Download images and save locally
- Print/process extracted data
Let's go through each of these steps in detail.
Imports
We need these frameworks:
#import <AFNetworking/AFNetworking.h>
#import "Ono.h"
Send HTTP Request
To download the web page:
NSURL *url = [NSURL URLWithString:@"<https://commons.wikimedia.org/wiki/List_of_dog_breeds>"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url.absoluteString headers:@{@"User-Agent": @"MyApp"}
success:^(NSURLSessionDataTask *task, id responseObject) {
// Parse HTML
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
We make a GET request and provide a custom user-agent header.
Parse HTML
To parse the HTML:
Ono *ono = [Ono onoWithHTML:responseObject];
The
Find Breed Table
We use a CSS selector to find the table element:
OnoNode *table = [ono find:@"table.wikitable.sortable"];
This selects the We loop through the rows: We iterate through Inside the loop, we get the column data: We use To download and save images: We download the image data and write it to a file. We store the extracted data: The arrays can then be processed as needed. And that's it! Here is the full code: This provides a complete Objective-C solution using AFNetworking and Ono to scrape data and images from HTML tables. The same approach can apply to many websites. While these examples are great for learning, scraping production-level sites can pose challenges like CAPTCHAs, IP blocks, and bot detection. Rotating proxies and automated CAPTCHA solving can help. Proxies API offers a simple API for rendering pages with built-in proxy rotation, CAPTCHA solving, and evasion of IP blocks. You can fetch rendered pages in any language without configuring browsers or proxies yourself. This allows scraping at scale without headaches of IP blocks. Proxies API has a free tier to get started. Check out the API and sign up for an API key to supercharge your web scraping. With the power of Proxies API combined with Python libraries like Beautiful Soup, you can scrape data at scale without getting blocked.
Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you
curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com" <!doctype html> Enter your email below to claim your free API key: with the required CSS classes.
Iterate Through Rows
for (OnoNode *row in [table find:@"tr"]) {
// Extract data
}
elements within the table. Extract Column Data
NSArray *cells = [row find:@"td, th"];
NSString *name = [[cells objectAtIndex:0] find:@"a"].text;
NSString *group = [cells objectAtIndex:1].text;
OnoNode *localNameNode = [cells objectAtIndex:2];
NSString *localName = localNameNode.find:@"span"].text ?: @"";
OnoNode *imgNode = [cells objectAtIndex:3];
NSString *photograph = [imgNode getAttribute:@"src"];
Download Images
if (photograph) {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:photograph]];
[imageData writeToFile:[NSString stringWithFormat:@"dog_images/%@.jpg", name] atomically:YES];
}
Store Extracted Data
[names addObject:name];
[groups addObject:group];
[localNames addObject:localName];
[photographs addObject:photograph];
// Imports
#import <AFNetworking/AFNetworking.h>
#import "Ono.h"
// Arrays to store data
NSArray *names = [NSArray array];
NSArray *groups = [NSArray array];
NSArray *localNames = [NSArray array];
NSArray *photographs = [NSArray array];
// Send HTTP request
NSURL *url = [NSURL URLWithString:@"<https://commons.wikimedia.org/wiki/List_of_dog_breeds>"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url.absoluteString headers:@{@"User-Agent": @"MyApp"}
success:^(NSURLSessionDataTask *task, id responseObject) {
// Parse HTML
Ono *ono = [Ono onoWithHTML:responseObject];
// Find table
OnoNode *table = [ono find:@"table.wikitable.sortable"];
// Iterate rows
for (OnoNode *row in [table find:@"tr"]) {
// Get cells
NSArray *cells = [row find:@"td, th"];
// Extract data
NSString *name = [[cells objectAtIndex:0] find:@"a"].text;
NSString *group = [cells objectAtIndex:1].text;
OnoNode *localNameNode = [cells objectAtIndex:2];
NSString *localName = localNameNode.find:@"span"].text ?: @"";
OnoNode *imgNode = [cells objectAtIndex:3];
NSString *photograph = [imgNode getAttribute:@"src"];
// Download image
if (photograph) {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:photograph]];
[imageData writeToFile:[NSString stringWithFormat:@"dog_images/%@.jpg", name] atomically:YES];
}
// Store data
[names addObject:name];
[groups addObject:group];
[localNames addObject:localName];
[photographs addObject:photograph];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
Browse by tags:
Browse by language:
The easiest way to do Web Scraping
Try ProxiesAPI for free
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
...Don't leave just yet!