Skip to main content

TestYourBond scrapper with python



Here we are going to make a web scrapper for Testyourbond , It's a website that provides quiz link to share with people and each one complete the quiz gets score out of 15.
Web scrapping is s a technique employed to extract data from websites . we are going to use this to get the correct answers of the quiz before doing it.so let's get started.


Making the quiz 

Here, how to create a quiz to get a link at the end . that's going to be our input to the script.
 1-
making quiz 1
2-
making quiz 2
3-
making quiz 3
4-
 

Now we have our link here and now we ready to code.

Setting up the environment

We are going to use python 3 with requests(it's used to get and post http requests to get data) and beautifulsoup(used for parsing the html and format it in data structured way) modules. you can download python 3 from here after this you can get the modules by typing this commends in terminal

pip install requests
pip install beautifulsoup4

Now we are ready to code.

Importing modules

import requests
from bs4 import BeautifulSoup 

Getting the URL

We will ask the user for input which is our generated link from the website , so we can make a get request to download the html document of the page.
 

page = requests.get(input("Enter the url of the quiz :"))

Parsing the html

Here comes the beautifulsoup turn to parse the html and formatting it. we are making a beautifulsoup object from the html document so we can use it's powerful methods.
 
soup=BeautifulSoup(page.content,"html.parser")

Getting all the questions 

We need to find a unique tag , class or id to search for and collect all the question text from it so by inspecting elements in browser and scrolling down we can find this block.
 
<div id="W05" class="question hidden unanswered">
<h3 class="fivepxtop">If TheBiggestNoob meet a genie, what would be TheBiggestNoob&#039;s wish?</h3>
<table class="pure-table pure-table-horizontal">
<tr>
<td value='a' class="answer incorrect">1 Million Dollar</td>
</tr>
<tr>
<td value='b' class="answer incorrect">Beautiful Wife/Handsome Husband</td>
</tr>
<tr>
<td value='c' class="answer correct">To be the PM of Country</td>
</tr>
<tr>
 <td value='d' class="answer incorrect">3 More Wishes</td>
</tr>
</table>
</div>

We can find a class "question hidden unanswered" that contain the question with h3 tag and class "fivepxtop". We can't search for "fivepxtop" class because there is others elements using it.
So we are going to search for h3 tag  inside the class of "question hidden unanswered" .

There is two methods in beautifulsoup find() that return first occurrence of a beautifulsoup object with attributes i search for and find_all() which return a list instead. so we are going to use find_all() to grap all the questions.

We will first search "question hidden unanswered" elements.


questions=soup.find_all(class_="question hidden unanswered")

Then searching in each element in the list for h3 tag and extract the text and storing it in a list.

question_text=[]
for item in questions:
 question_text.append(item.find('h3').get_text())

Now we have collected all the questions. let's search for the answers.

Getting all the correct answers

 We will do the same we did before this timer it's easier because when we search in the html document we can find that there is a unique class the contain the correct answer.
 
<tr>
<td value='c' class="answer correct">To be the PM of Country</td>
</tr>

As we can see class "answer correct" which we will search for and extract the text and storing it in a list.

answers=soup.find_all(class_="answer correct")
answer_text=[]
for item in answers:
 answer_text.append(item.get_text())

Making the output

displaying the output in a easy way for our user.
 
for i in range(0,15):
 print(str(i+1)+"-Q: "+question_text[i]+" A: "+answer_text[i]+"\n")

We have finished our script, you can get it from here . and it's ready to test.




Comments

Popular posts from this blog

My Roadmap part 4

Electronics... Yikes! I told you previously that I have enough curiosity to dive into anything related to programming. Guess what, electronics and the world of embedded systems are now among them! It all started with participating in robotics competitions. The organizers told us that, to make a robot, you need to combine mechanical design with control design, and they gave us sessions on both. I barely cared for those sessions, as you know, I’m driven by a self-learning mindset (“I gotta do it my way!”). My first event was the Under-Construction competition organized by the IEEE Helwan Student Branch. I only had experience in programming and making games, and I was tutoring game-development sessions at the same time. I treated hardware the way I treat software: a trial-and-error strategy. Unfortunately, I realized the drawbacks a bit late, after burning countless components and, basically, burning money like crazy. “Let’s hit run and see.” Even when wiring: “Let’s wire and see.” I was ...

My Roadmap part 1

It all started with a dream... I was playing games all of my life and was good at it so why not make them. That's what I thought when I heard for the first time that you can make your own games with something called Programming Language. At first, It was c++ I learned it and was practicing it a lot, solving problems and automating stuff like solving math equations for assignments. I gained some confidence and It was time to make games and put some challenges. Solving equations Cardians method, Guessing the number game I started by making console games (x-o, Sudoku, Word Jumble). I was really proud of this *YaY* then started showing it to my friends. The only thing they said was "what is this black box, where is the game?".after that I decided it's time to learn how to make graphics with c++. Word Jumble, Sudoku Game After doing some searching, I found something called "SFML( standard fast multimedia library)" which I can use with c...

My Roadmap part 2

A Journey to the 3D world. This stage in my life, I have decided to learn about the 3D world before creating 3D games so When I start making games I have a robust understanding of the models. So I started to read about it. The first thing was the pipeline of the creation of the 3d models( Concept Art - Modeling - Texturing - Rigging - Animation - rendering ). I didn't like the Art section neither was good at it so I decided to go directly to the modeling part and just try to clone objects or do whatever comes to my mind. I followed step by step tutorials and try to get used to its interface. It was Maya Autodesk 2016. My First model was a glass. It took 2 hours from me to do it alone after that, I couldn't find the button for saving the render result as an image so I took screenshot *lol*. My First Model After a lot of practicing and watching tutorials following the steps. I got a basic understanding of making models out of simple shapes and just colorin...