-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
77 lines (58 loc) · 3.25 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
69
70
71
72
73
74
75
76
77
from flask import Flask, render_template, request,jsonify
from flask_cors import CORS,cross_origin
import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen as uReq
import logging
import pymongo
logging.basicConfig(filename="scrapper.log" , level=logging.INFO)
import os
app = Flask(__name__)
@app.route("/", methods = ['GET'])
def homepage():
return render_template("index.html")
@app.route("/review" , methods = ['POST' , 'GET'])
def index():
if request.method == 'POST':
try:
# query to search for images
query = request.form['content'].replace(" ","")
# directory to store downloaded images
save_directory = "images/"
# create the directory if it doesn't exist
if not os.path.exists(save_directory):
os.makedirs(save_directory)
# fake user agent to avoid getting blocked by Google
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"}
# fetch the search results page
response = requests.get(f"https://www.google.com/search?q={query}&sxsrf=AJOqlzUuff1RXi2mm8I_OqOwT9VjfIDL7w:1676996143273&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiq-qK7gaf9AhXUgVYBHYReAfYQ_AUoA3oECAEQBQ&biw=1920&bih=937&dpr=1#imgrc=1th7VhSesfMJ4M")
# parse the HTML using BeautifulSoup
soup = BeautifulSoup(response.content, "html.parser")
# find all img tags
image_tags = soup.find_all("img")
# download each image and save it to the specified directory
del image_tags[0]
img_data=[]
for index,image_tag in enumerate(image_tags):
# get the image source URL
image_url = image_tag['src']
#print(image_url)
# send a request to the image URL and save the image
image_data = requests.get(image_url).content
mydict={"Index":index,"Image":image_data}
img_data.append(mydict)
with open(os.path.join(save_directory, f"{query}_{image_tags.index(image_tag)}.jpg"), "wb") as f:
f.write(image_data)
# client = pymongo.MongoClient("mongodb+srv://snshrivas:Snshrivas@cluster0.ln0bt5m.mongodb.net/?retryWrites=true&w=majority")
# db = client['image_scrap']
# review_col = db['image_scrap_data']
# review_col.insert_many(img_data)
return "image laoded"
except Exception as e:
logging.info(e)
return 'something is wrong'
# return render_template('results.html')
else:
return render_template('index.html')
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000)