Web Scraper with Python (Beautiful Soup) & Deployment of it into Heroku [Part1]
Tags: webscrapping, python, beautifulsoup
Published: 2022-09-01 18:38:17

A while ago I decided to create a web crawling project using [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/) (a Python library for pulling data out of HTML and XML files). Here is how I did it, hurdles I faced during development and how I overcame them. 

![Meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crxmto0ixmnbuoje90j0.jpg)<br/>
We will use [Virtual Environment](https://docs.python.org/3/library/venv.html#:~:text=A%20virtual%20environment%20is%20a,part%20of%20your%20operating%20system.) throughout the development, [here are](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/) the instructions on how to install it in Windows and [here are](https://realpython.com/python-virtual-environments-a-primer/#why-do-you-need-virtual-environments) the reasons.
![Install virual environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mwk41wgqskzkh22q26r5.png)

To deactivate your virtual Environment simply type `deactivate`
![To deactivate your virtual Environment simply type deactivate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dhii4555cc2t5txo8xb.png)

Then we will create a `requirements.txt` file for listing all the dependencies for our Python project.
![requests.txt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/248i80vi00647u4ib178.png)Your requirements might differ depending on your case.

`requirements.txt` is **important!** I was too lazy to do this step at the first attempt... However, sooner or later you have to do it at least in order to push it into Heroku.
`pip install -r requirements.txt` is the command to install the list of requirements.
<br/>
Now let me show to you how to write code to actually scrap the given web site. In a nutshell, web scraping is extracting data from websites into the form of your choice (I wrote "https://www.scrapethissite.com/pages/" to csv file).

![website => csv](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8w3jvakc61owq84hskg9.png)
![web scrapping](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dam9pconadel2s30eyo3.png)

<br/>
First we make web requests using python [requests](https://pypi.org/project/requests/) library.

As you can see below, we printed the content of received information and it is the same HTML page, which you can see with the keyboard combination of Ctrl+U in Chrome or by pressing the right click on your mouse, then View page source.
![Way to source code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzcotj0y3x0dv6zfkuy8.png)

```
import requests
from bs4 import BeautifulSoup

link = "https://www.scrapethissite.com/pages/"
request = requests.get(link)
print(request.content)
```

![Source Code in Terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/id0stvjs5fv67r9g26qe.png)
![Source Code Chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ql2wahw2glvxuem1cvcb.png)

To withdraw actual data from HTML tags we are going to reach for the help of Beautiful Soup library.

Get `.text` from `<title>` tag:

```
import requests
from bs4 import BeautifulSoup

link = "https://www.scrapethissite.com/pages/"
request = requests.get(link)

soup = BeautifulSoup(request.content, "html5lib")
print(soup.title.text)
```

![Out put of soup.title.text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca20j4089pnbsxnbct6r.png)


Withdraw hyperlinks with `.a` tag:
```
import requests
from bs4 import BeautifulSoup

link = "https://www.scrapethissite.com/pages/"
request = requests.get(link)

soup = BeautifulSoup(request.content, "html5lib")
print(soup.a)
```
`.find_all()`:
```
import requests
from bs4 import BeautifulSoup

link = "https://www.scrapethissite.com/pages/"
request = requests.get(link)

soup = BeautifulSoup(request.content, "html5lib")

for i in soup.find_all('h3'):
    print(i.text)
```
![.find_all](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97k07j5atxos0gyj6rm2.png)


You can even search with CSS class `.find_all(class_="class_name")`
```
import requests
from bs4 import BeautifulSoup

link = "https://www.scrapethissite.com/pages/"
request = requests.get(link)

soup = BeautifulSoup(request.content, "html5lib")

for i in soup.find_all(class_='class_name'):
    print(i.text)
```

![class_=''](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0vp2vk9m9guo3cfs9sv.png)



<br/>
The rule of thumb here is to find the piece of data from the source code of the web site (via Ctrl+F in Chrome) and extract the data using whatever tag it is in.
![Ctrl + F](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdegun0g03nofqkha4xg.png)
<br/>
There are many tags on [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#), and from my experience what I found out is, often tutorials or/and posts are not perfectly suitable for your case. Reading it on post sounds like I am shooting myself in the foot, doesn't it? :sweat_smile: Do not get me wrong posts/videos are by all means useful to get a general idea about the topic. Nonetheless, if you are working on a different situation it is better if you skim the docs, so you can tackle your problem with more adequate methods. Besides, by the time you are watching/reading the video tutorial/post things (versions) are very likely to be changed. So what I would suggest is going to what initially seems a hard way and read the documentary, rather than trying to cut the corners and ending up frustrated with wasted time.   
Furthermore, if you need to insert scraped data into database in your local machine I would recommend you a [real python](https://realpython.com/python-sql-libraries/#understanding-the-database-schema) article. 
In the next part we will see how I pushed scrapper into Heroku Server and how to build a database there.
 
You can find the source code on my Git Hub page: https://github.com/Muxsidov/Scraper_Blog