-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (57 loc) · 3.58 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Note: Tags in the flipkart website can sometimes change. So Inspect particular tag in line 27 and 39 if the code does not give output
# doing necessary imports
from flask import Flask, render_template, request, jsonify
import requests
from bs4 import BeautifulSoup as bs
from urllib.request import urlopen as uReq
app = Flask(__name__) # initialising the flask app with the name 'app'
@app.route('/', methods=['GET'])
def homepage():
return render_template('index.html')
# Creating Post method
@app.route('/scrap', methods=['POST']) # route with allowed methods as POST and GET
def index():
if request.method == 'POST':
searchString = request.form['content'].replace(" ", "") # obtaining the search string entered in the form
try:
flipkart_url = "https://www.flipkart.com/search?q=" + searchString # preparing the URL to search the product on flipkart
uClient = uReq(flipkart_url) # requesting the webpage from the internet
flipkartPage = uClient.read() # reading the webpage
uClient.close() # closing the connection to the web server
flipkart_html = bs(flipkartPage, "html.parser") # parsing the webpage as HTML
bigboxes = flipkart_html.findAll("div", {"class": "_1AtVbE col-12-12"}) # seacrhing for appropriate tag to redirect to the product link
del bigboxes[0:3] # the first 3 members of the list do not contain relevant information, hence deleting them.
box = bigboxes[0] # taking the first iteration (for demo)
productLink = "https://www.flipkart.com" + box.div.div.div.a['href'] # extracting the actual product link
prodRes = requests.get(productLink) # getting the product page from server
prod_html = bs(prodRes.text, "html.parser") # parsing the product page as HTML
commentboxes = prod_html.find_all('div', {
'class': "_16PBlm"}) # finding the HTML section containing the customer comments
reviews = [] # initializing an empty list for reviews
# iterating over the comment section to get the details of customer and their comments
for commentbox in commentboxes:
try:
name = commentbox.div.div.find_all('p', {'class': '_2sc7ZR _2V5EHH'})[0].text # Search for the Name of the Person. This name tag conatains reviews written by customers
except:
name = 'No Name'
try:
rating = commentbox.div.div.div.div.text # Seres of div are given to select the specific box which contains the text
except:
rating = 'No Rating'
try:
commentHead = commentbox.div.div.div.p.text
except:
commentHead = 'No Comment Heading'
try:
comtag = commentbox.div.div.find_all('div', {'class': ''})
custComment = comtag[0].div.text
except:
custComment = 'No Customer Comment'
mydict = {"Product": searchString, "Name": name, "Rating": rating, "CommentHead": commentHead,
"Comment": custComment} # saving that detail to a dictionary
reviews.append(mydict) # appending the comments to the review list
return render_template('results.html', reviews=reviews) # showing the review to the user
except:
return 'something is wrong'
if __name__ == "__main__":
app.run(port=8000, debug=True) # running the app on the local machine on port 8000