Skip to content

Instantly share code, notes, and snippets.

@maxking
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxking/40589b811c657af146c3 to your computer and use it in GitHub Desktop.
Save maxking/40589b811c657af146c3 to your computer and use it in GitHub Desktop.
Given URL of the first page of a category from Indiabix.com, downloads all the questions with answers.
"""
This script downloads all the questions from Indiabix.com from the url of a given category.
It can easily be customized (if you know basic python) to download all the questions from
all the categories. It also downloads their answers.
Author: Abhilash Raj <raj.abhilash1@gmail.com>
Date: 17th March 2015
Copyright (c) 2015 Abhilash Raj <Abhilash Raj>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
import re
from ghost import Ghost
from bs4 import BeautifulSoup
Ans = {'A': 1,
'B': 2,
'C': 3,
'D': 4}
# HOST =
# PORT =
def get_html(url):
gh = Ghost(download_images=False,wait_timeout=100)
# uncomment the line below if you access internet through a proxy.
# Do not forget to first update the values of HOST, and PORT above.
# gh.set_proxy('http', HOST, PORT)
page, response = gh.open(url)
return gh.content
def print_qa(soup):
for table in soup.find_all("table", class_="bix-tbl-container"):
print table.find("td", class_="bix-td-qtxt").get_text()
for option, value in enumerate(table.find_all("td",
class_="bix-td-option",
width="49%"),
start=1):
print option,'.', value.get_text()
print "Answer:", Ans[table.find("input", type="hidden").get("value")]
print ""
if __name__ == "__main__":
BASE_URL = 'http://www.indiabix.com'
url = sys.argv[1]
next_tag = 1
while next_tag:
html = get_html(url)
soup = BeautifulSoup(html)
print(print_qa(soup))
try:
# Search the next url, break if there is no "Next" url.
next_tag = soup.find("a", text=re.compile("Next"))
url = BASE_URL + next_tag.get('href')
except AttributeError:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment