Interview Question
Code:
# Parse https://metacritic.com/game/playstation-4
import json
import re
from bs4 import BeautifulSoup;
import requests;
from flask import Flask;
from flask import jsonify;
# Webserver IP and port.
host = "127.0.0.1";
port = 5001;
# Initialize Flask app
app = Flask(__name__);
url="https://www.metacritic.com/game/playstation-4";
#response = requests.get(url);
#print (response); # If the response code is 403 then use user_agent = {'User-agent': 'Mozilla/5.0'}
user_agent = {'User-agent': 'Mozilla/5.0'};
def webscrapper():
response = requests.get(url, headers = user_agent);
#print(response); # This would return 200
# Parse the html file and save the objects
#print(response.text);
soup = BeautifulSoup(response.text, 'html.parser');
#print (soup);
# Create a dictionary to save the title and score.
title = [];
score = [];
for i in soup.findAll('a',{"class": "title"}):
title.append(i.get_text(strip=True));
for i in soup.findAll('div',{"class": "clamp-score-wrap"}):
score.append(i.get_text(strip=True));
# Now lets convert both the list to dict with keys title and score using zip method
review = [{'title':x ,"score":y} for x,y in zip(title,score)];
# The indent parameter specifies the spaces that are used at the beginning of a line.
return json.dumps(review, indent=2); # Flask cannot return list. Only dict, string, tuple
def titlefilter(title):
temp = webscrapper();
error = "Title Not Found.";
if title in temp:
temp = json.loads(temp);
# Load the json as list
for i in temp:
if i.get("title") == title:
return (json.dumps(i, indent=2));
else:
return jsonify(message=error);
@app.route('/games')
def review():
return webscrapper();
@app.route('/games/<title>')
def titfilter(title):
return titlefilter(title);
if __name__ == "__main__":
app.run(debug=True);
Comments
Post a Comment