Create your first web scraper with Scraper API and Python
Tags: posts, python, scraping
Published: 2019-10-28 18:46:14

---
title: Create your first web scraper with Scraper API and Python
published: true
date: 2019-06-30 19:47:50 UTC
tags: Posts,Python,scraping
canonical_url: http://blog.adnansiddiqi.me/create-your-first-web-scraper-with-scraper-api-and-python/
---

Recently I come across a tool that takes care of many of the issues you usually face while scraping websites. The tool is called [Scraper API](https://www.scraperapi.com/?fp_ref=adnan22) which provides an easy to use REST API to scrape a different kind of websites(Simple, JS enabled, Captcha, etc) with quite an ease. Before I proceed further, allow me to introduce about _Scraper API_.

## What is Scraper API

If you visit their [website](https://www.scraperapi.com/?fp_ref=adnan98) you’d find their mission statement:

> _Scraper API handles proxies, browsers, and CAPTCHAs, so you can get the HTML from any web page with a simple API call!_

![](http://blog.adnansiddiqi.me/wp-content/uploads/2019/06/Screen-Shot-2019-06-27-at-12.25.17-PM.png)

As it suggests, it is offering you all the things to deal with the issues you usually come across while writing your scrapers.

## Development

Scraper API provides a REST API that can be consumed in any language. Since this post is related to Python so I’d be mainly focusing on `requests` library to use this tool.

You must first [signup](https://www.scraperapi.com/) with them and in return, they will provide you an API KEY to use their platform. They provide 1000 free API calls which are enough to test their platform. Otherwise, they offer different plans from starter to the enterprise which you can view [here](https://www.scraperapi.com/pricing).

Let’s try a simple example which is also giving in the [documentation](https://www.scraperapi.com/documentation).

```
import requests if \_\_name\_\_ == '\_\_main\_\_': API\_KEY = '\<YOUR API KEY\>' URL\_TO\_SCRAPE = 'https://httpbin.org/ip' payload = {'api\_key': API\_KEY, 'url': URL\_TO\_SCRAPE} r = requests.get('http://api.scraperapi.com', params=payload, timeout=60) print(r.text)
```

Assuming you are registered and have got an API which you can find on the [dashboard](https://www.scraperapi.com/dashboard), you can start working right away after having it. When you run this program it shows the IP address of your request.

![](http://blog.adnansiddiqi.me/wp-content/uploads/2019/06/Screen-Shot-2019-06-27-at-12.56.43-PM.png)

Do you see, every time it returns a new IP address, cool, isn’t it?

There are some scenarios where you would like to use the same proxy to give the impression that a single user is visiting a different part of the website. For that, you can pass `session_number` parameter in the `payload` variable above.

```
URL\_TO\_SCRAPE = 'https://httpbin.org/ip' payload = {'api\_key': API\_KEY, 'url': URL\_TO\_SCRAPE,'session\_number': '123'} r = requests.get('http://api.scraperapi.com', params=payload, timeout=60) print(r.text)
```

And it’d produce the following result:

[![](http://blog.adnansiddiqi.me/wp-content/uploads/2019/06/Screen-Shot-2019-06-28-at-7.33.58-PM-300x130.png)](http://blog.adnansiddiqi.me/wp-content/uploads/2019/06/Screen-Shot-2019-06-28-at-7.33.58-PM.png)

Can you notice the same proxy IP here?

## Creating OLX Scrapper

Like previous [scraping related posts](http://blog.adnansiddiqi.me/tag/scraping/), I am going to pick OLX again for this post. I will iterate the list first and then will scrape individual items. Below is the complete code.

```
payload = {'api\_key': API\_KEY, 'url': URL\_TO\_SCRAPE, 'session\_number': '123'} r = requests.get('http://api.scraperapi.com', params=payload, timeout=60) if r.status\_code == 200: html = r.text.strip() soup = BeautifulSoup(html, 'lxml') links = soup.select('.EIR5N \> a') for l in links: all\_links.append('https://www.olx.com.pk' + l['href']) idx = 0 if len(all\_links) \> 0: for link in all\_links: sleep(5) payload = {'api\_key': API\_KEY, 'url': link, 'session\_number': '123'} if idx \> 1: break r = requests.get('http://api.scraperapi.com', params=payload, timeout=60) if r.status\_code == 200: html = r.text.strip() soup = BeautifulSoup(html, 'lxml') price\_section = soup.find('span', {'data-aut-id': 'itemPrice'}) print(price\_section.text) idx += 1
```

I am using `Beautifulsoup` to parse HTML. I have only extracted Price here because the purpose is to tell about the API itself than _Beautifulsoup_. You should see my post [here](https://dev.to/kadnan/write-your-first-web-scraper-in-python-with-beautifulsoup-p2b) in case you are new into scraping and Python.

## Conclusion

In this post, you learned how to use Scraper API for scraping purpose. Whatever you can do with this API you can do it by other means as well; this API provides you everything under the umbrella, especially rendering of pages via Javascript for which you need headless browsers which, at times become cumbersome to set things up on remote machines for headless scraping. Scraper API is taking care of it and charging nominal charges for individuals and enterprise. The company I work with spend 100s of dollars on a monthly basis just for the proxy IPs.

Oh if you sign up [here](https://www.scraperapi.com/pricing?fp_ref=adnan98) with my referral link or enter promo code **SCRAPE156980** , you will get **10% discount** on it. In case you do not get the discount then just let me know via email on my site and I’d sure help you out.

In coming days I’d be writing more posts about Scraper API discussing further features.