![]() |
VOOZH | about |
In this article, we are going to see how to build a web scraping bot in Python.
Web Scraping is a process of extracting data from websites. A Bot is a piece of code that will automate our task. Therefore, A web scraping bot is a program that will automatically scrape a website for data, based on our requirements.
pip install bs4
pip install requests
pip install selenium
We need to install a chrome driver to automate using selenium, our task is to create a bot that will be continuously scraping the google news website and display all the headlines every 10mins.
Step 1: First we will import some required modules.
Step 2: The next step is to open the required website.
Output:
👁 ImageStep 3: Extracting the news title from the webpage, to extract a specific part of the page, we need its XPath, which can be accessed by right-clicking on the required element and selecting Inspect in the dropdown bar.
👁 ImageAfter clicking Inspect a window appears. From there, we have to copy the elements full XPath to access it:
👁 ImageNote: You might not always get the exact element that you want by inspecting (depends on the structure of the website), so you may have to surf the HTML code for a while to get the exact element you want. And now, just copy that path and paste that into your code. After running all these lines of code, you will get the title of the first heading printed on your terminal.
Output:
'Attack on Afghan territory': Taliban on US airstrike that killed 2 ISIS-K men
Step 4: Now, the target is to get the X_Paths of all the headlines present.
One way is that we can copy all the XPaths of all the headlines (about 6 headlines will be there in google news every time) and we can fetch all those, but that method is not suited if there are a large number of things to be scrapped. So, the elegant way is to find the pattern of the XPaths of the titles which will make our tasks way easier and efficient. Below are the XPaths of all the headlines on the website, and let's figure out the pattern.
/html/body/c-wiz/div/div[2]/div[2]/div/main/c-wiz/div[1]/div[3]/div/div/article/h3/a
/html/body/c-wiz/div/div[2]/div[2]/div/main/c-wiz/div[1]/div[4]/div/div/article/h3/a
/html/body/c-wiz/div/div[2]/div[2]/div/main/c-wiz/div[1]/div[5]/div/div/article/h3/a
/html/body/c-wiz/div/div[2]/div[2]/div/main/c-wiz/div[1]/div[6]/div/div/article/h3/a
/html/body/c-wiz/div/div[2]/div[2]/div/main/c-wiz/div[1]/div[7]/div/div/article/h3/a
/html/body/c-wiz/div/div[2]/div[2]/div/main/c-wiz/div[1]/div[8]/div/div/article/h3/a
So, by seeing these XPath's, we can see that only the 5th div is changing (bolded ones). So based upon this, we can generate the XPaths of all the headlines. We will get all the titles from the page by accessing them with their XPath. So to extract all these, we have the code as
Output:
👁 ImageNow, the code is almost complete, the last thing we have to do is that the code should get headlines for every 10 mins. So we will run a while loop and sleep for 10 mins after getting all the headlines.
Below is the full implementation
Output:
👁 ImageThe requests module gets the raw HTML data from websites and beautiful soup is used to parse that information clearly to get the exact data we require. Unlike Selenium, there is no browser installation involved and it is even lighter because it directly accesses the web without the help of a browser.
Step 1: Import module.
Step 2: The next thing to do is to get the URL data and then parse the HTML code
Step 3: First, we shall get all the headings from the table.
Output:
👁 ImageStep 4: In the same way, all the values in each row can be obtained
Output:
👁 ImageBelow is the full implementation:
Output:
👁 ImageThis is a specific method, used to run the bot continuously online without the need for any human intervention. replit.com is an online compiler, where we will be running the code. We will be creating a mini webserver with the help of a flask module in python that helps in the continuous running of the code. Please create an account on that website and create a new repl.
👁 ImageAfter creating the repl, Create two files, one to run the bot code and the other to create the web server using flask.
Code for cryptotracker.py:
Code for the keep_alive.py (webserver):
Keep-alive is a method in networking that is used to prevent a certain link from breaking. Here the purpose of the keep-alive code is to create a web server using flask, that will keep the thread of the code (crypto-tracker code) to be active so that it can give the updates continuously.
👁 ImageNow, we have a web server create, and now, we need something to ping it continuously so that the server does not go down and the code keeps on running continuously. There is a website uptimerobot.com that does this job. Create an account in it
👁 ImageRunning the Crypto tracker code in Replit. Thus, We have successfully created a web scraping bot that will scrap the particular website continuously for every 10 mins and print the data to the terminal.