Created
January 22, 2024 21:17
-
-
Save Mianto/5d404956ed40244acd7f693e2c127dae to your computer and use it in GitHub Desktop.
Scraping Question & Answers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
import requests | |
import csv | |
def main(): | |
print("Scraping Begin") | |
print("--------------------------------") | |
qna = {} | |
for i in range (46): | |
url = "https://www.gkseries.com/mass-communication-and-journalism-questions-answers/" + str(i) + "-mass-communication-and-journalism-multiple-choice-questions-and-answers" | |
print(url) | |
page = requests.get(url=url) | |
soup = BeautifulSoup(page.text, 'html.parser') | |
for mcq in soup.find_all("div", class_="mcq"): | |
question = mcq.find("div", class_="question-content") | |
stripped_question = "\n".join(question.text.strip().splitlines()[2:]) | |
print(stripped_question) | |
ans = mcq.find("blockquote", class_="blockquote") | |
print(ans.text) | |
qna[stripped_question] = ans.text.strip() | |
print("Scraping End") | |
print("--------------------------------") | |
print("Save to CSV") | |
with open('dict.csv', 'w') as csv_file: | |
writer = csv.writer(csv_file) | |
for key, value in qna.items(): | |
writer.writerow([key, value]) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment