Skip to main content

Web scrapping: things you will not know before getting your hand dirty

What is Web scrapping?

Web scrapping is a important part of data science. Data science is not only use and analysis of the already created, usable data; but also part of it is to search and create that data. Many times, you will come across a general question in data science field for which you may not find a ready made data in CSV or JSON. So, the solution is Web scrapping. 

Web scrapping means to scrap the web, i.e. programatically extracting information needed from one or more than one web page. Web scrapping also goes into detailed soft-wares named web crawlers or so called spiders.  

Web scrapping can be done in many languages. For this and the coming posts, I am going to use python for web scrapping. I will show examples where the easiest approach for web scrapping works and where it does not. Also, on later posts, we will explore a bit complex methods and modules to do web scrapping.

Introduction to methods:

There are broadly two methods to do web scraping. One is using simple http request to access the sites and then mine them. The second one, far more complicated, is using the automation tool selenium; which is mainly used for web application testing.

(1) First process, at least for python, you can scrap the webpage using a library called "BeautifulSoup". This is a very useful library and mainly used for web scraping. I will not go into a detailed documentation kind of description of the same, but if one is interested, one can find here

Now, I will provide code snippets and describe different scenarios about web scraping. First, we will go on to scrap reddit.com; which is, being kind enough, lets you scrap the page. 


here, if you notice the code throughly; you can see that, I have used the urllib library of python 3 here. Please be aware to use the python 3 version libraries in your code if you are using python 3; otherwise, in case of python 2, this library is something different which I am not going to mention.
urllib.request is the python package used to send requests to the sites for access. Other libraries are out of scope for this current post. But, if you want to work with more on these url requesting topic, you must definitely check out urllib modules.

Now, here, I have created a variable named page_url, where I have put the website url. Now, putting this variable as the parameter for urllib.request.urlopen() opens the corresponding webpage, and then saves the source in the variable to which the function returns.

Now, we will use BeautifulSoup module. Note that, this has to be imported from the bs4 module further.
In beautifulsoup, there are multiple html parsers available; i.e. which are specifically suitable for HTML5, XML files etc. Here, we are going to use the html.parser to parse the downloaded html file.

Now, this is important to understand that, although the webpage source gets downloaded in the page variable in the above code; but it is not at all easy to find any link or text out of the html and CSS mess which will get downloaded in that.
Also, there occurs number of issues with the webpages when it detects a machine request; but more on that later; specifically, we will discuss the different problems before getting into  selenium, to show the importance of selenium in avoiding all those problems.

Whatever, so, in the next cell, we parse through the downloaded page source using BeautifulSoup and create a soup object out of it.
To see the source with proper indentation; you can write:
print(soup.prettify())
which basically prettifies the source code and then prints it in a well manner.

This, soup, is a object where each divs and tags are children of their parent classes and therefore the whole html-css code forms a tree like object , which is the soup.
As web scraping is too deep and if one starts to show examples of doing the work then one will have to write a book; so I will stay in the scope of the post, and will keep it brief.

We will show, how to find basic tags and then at the end of this post, we will show a piece of code, which can take user input and sum up a paragraph on that, scraping the wikipedia.

Searching tags and classes:

(1)
For searching specific tags, i.e. breaks (</br>) , paragraphs (<p>) , divs (<div>) etc, you have to use the function soup.find() and  soup.find_all().
i.e. observe the following code snippet:

Notice the level at which the html code can look cryptic when it comes for a commercial website. The above code, searches for 'a' in the tags, and therefore, it effectively finds out all the <a href> references.

(2)
For finding something specific, you can use the find function; i.e.
soup.find(attr={#something})

this  will give you a result of one such attributed class or tag from the html source code.

(3)
To find some tag/class with specific mentions like a div with specific class name "Ih30" or something like that, you have to use the same type of find function but then the format changes to soup.find_all(class_name,{attr: specific name }) where the attribute will be inside that class named tag.
Look at the code below to understand one such implementation:

(4)
To find the text written in between the opening part and the closing part of a tag, you can use the following format:
soup.tag.text.strip() which will give you the text inside the tags. Without using the strip(), you would get teh tags attached to the text, which is of no good.

Also, there are multiple ways of parsing from one element to the next in the tree structure of soup. But we will not get into that.

We have explored the surface of use of beautifulsoup; and how to find tags and classes.

The paragraph creator

Now, I will show a small application to create a paragraph about anything out of wikipedia:
first see the code below on your own and try to figure out.
I hope that uptil now, the first four lines are clear from above description. Now, the text variable basically is a list of all the paragraph tags. Then we initiate a empty string.
Now, as we have discussed earlier, in each loop, the num refers to one paragraph tag, and the loop, at each go, takes one para tag, strip it from the html additions, and then we add it with the string. At end of each looping, we add three empty lines.
In this above code snippet, we have hard coded the website "https://en.wikipedia.org/wiki/Pattern". In a better code, we will want to search just by putting the keyword.
This, in turn, is still in development for me.

Conclusion:

For scraping websites, you will need good understanding of HTML, CSS and basic javascript; and then using BeautifulSoup and urllib library of python, one can really download source of the webpage, search for tags, texts, images and links and therefore properly derive data from it. 
In the next post, we will discuss about problems with this simplistic approach and therefore extend to selenium. The post exploring selenium is here
Thanks for reading! 

Comments

Popular posts from this blog

Tinder bio generation with OpenAI GPT-3 API

Introduction: Recently I got access to OpenAI API beta. After a few simple experiments, I set on creating a simple test project. In this project, I will try to create good tinder bio for a specific person.  The abc of openai API playground: In the OpenAI API playground, you get a prompt, and then you can write instructions or specific text to trigger a response from the gpt-3 models. There are also a number of preset templates which loads a specific kind of prompt and let's you generate pre-prepared results. What are the models available? There are 4 models which are stable. These are: (1) curie (2) babbage (3) ada (4) da-vinci da-vinci is the strongest of them all and can perform all downstream tasks which other models can do. There are 2 other new models which openai introduced this year (2021) named da-vinci-instruct-beta and curie-instruct-beta. These instruction models are specifically built for taking in instructions. As OpenAI blog explains and also you will see in our

Can we write codes automatically with GPT-3?

 Introduction: OpenAI created and released the first versions of GPT-3 back in 2021 beginning. We wrote a few text generation articles that time and tested how to create tinder bio using GPT-3 . If you are interested to know more on what is GPT-3 or what is openai, how the server look, then read the tinder bio article. In this article, we will explore Code generation with OpenAI models.  It has been noted already in multiple blogs and exploration work, that GPT-3 can even solve leetcode problems. We will try to explore how good the OpenAI model can "code" and whether prompt tuning will improve or change those performances. Basic coding: We will try to see a few data structure coding performance by GPT-3. (a) Merge sort with python:  First with 200 words limit, it couldn't complete the Write sample code for merge sort in python.   def merge(arr, l, m, r):     n1 = m - l + 1     n2 = r- m       # create temp arrays     L = [0] * (n1)     R = [0] * (n

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like knowle