Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
# Custom CSS to mimic Google's interface
|
| 6 |
+
def load_css():
|
| 7 |
+
st.markdown("""
|
| 8 |
+
<style>
|
| 9 |
+
body {
|
| 10 |
+
font-family: Arial, sans-serif;
|
| 11 |
+
background-color: #f8f9fa;
|
| 12 |
+
}
|
| 13 |
+
.search-bar {
|
| 14 |
+
width: 50%;
|
| 15 |
+
margin: 20px auto;
|
| 16 |
+
display: flex;
|
| 17 |
+
align-items: center;
|
| 18 |
+
border: 1px solid #dfe1e5;
|
| 19 |
+
border-radius: 24px;
|
| 20 |
+
padding: 10px;
|
| 21 |
+
background-color: #fff;
|
| 22 |
+
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
|
| 23 |
+
}
|
| 24 |
+
.search-bar input {
|
| 25 |
+
flex: 1;
|
| 26 |
+
border: none;
|
| 27 |
+
outline: none;
|
| 28 |
+
font-size: 16px;
|
| 29 |
+
padding: 5px;
|
| 30 |
+
}
|
| 31 |
+
.search-bar button {
|
| 32 |
+
background: none;
|
| 33 |
+
border: none;
|
| 34 |
+
color: #4285f4;
|
| 35 |
+
font-size: 18px;
|
| 36 |
+
cursor: pointer;
|
| 37 |
+
}
|
| 38 |
+
.search-result {
|
| 39 |
+
margin: 20px auto;
|
| 40 |
+
width: 80%;
|
| 41 |
+
padding: 15px;
|
| 42 |
+
border: 1px solid #dfe1e5;
|
| 43 |
+
border-radius: 8px;
|
| 44 |
+
background-color: #fff;
|
| 45 |
+
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
|
| 46 |
+
}
|
| 47 |
+
.search-result h3 {
|
| 48 |
+
margin: 0;
|
| 49 |
+
color: #1a0dab;
|
| 50 |
+
font-size: 18px;
|
| 51 |
+
}
|
| 52 |
+
.search-result p {
|
| 53 |
+
margin: 5px 0;
|
| 54 |
+
color: #545454;
|
| 55 |
+
font-size: 14px;
|
| 56 |
+
}
|
| 57 |
+
.captcha-warning {
|
| 58 |
+
text-align: center;
|
| 59 |
+
margin-top: 50px;
|
| 60 |
+
}
|
| 61 |
+
.captcha-warning h3 {
|
| 62 |
+
color: #d93025;
|
| 63 |
+
}
|
| 64 |
+
</style>
|
| 65 |
+
""", unsafe_allow_html=True)
|
| 66 |
+
|
| 67 |
+
# Function to perform a Google search
|
| 68 |
+
def search_google(query, proxy=None):
|
| 69 |
+
headers = {
|
| 70 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 71 |
+
}
|
| 72 |
+
url = f'https://www.google.com/search?q={query.replace(" ", "+")}&hl=en'
|
| 73 |
+
proxies = {"http": proxy, "https": proxy} if proxy else None
|
| 74 |
+
response = requests.get(url, headers=headers, proxies=proxies, timeout=10)
|
| 75 |
+
if "detected unusual traffic" in response.text.lower(): # Detect CAPTCHA
|
| 76 |
+
return None, True
|
| 77 |
+
return response.text, False
|
| 78 |
+
|
| 79 |
+
# Function to parse Google search results
|
| 80 |
+
def parse_search_results(html_content):
|
| 81 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
| 82 |
+
results = []
|
| 83 |
+
for g in soup.find_all('div', class_='tF2Cxc'):
|
| 84 |
+
title = g.find('h3').text if g.find('h3') else "No title"
|
| 85 |
+
link = g.find('a')['href'] if g.find('a') else "No link"
|
| 86 |
+
snippet = g.find('span', class_='aCOpRe').text if g.find('span', class_='aCOpRe') else "No snippet"
|
| 87 |
+
results.append({'title': title, 'link': link, 'snippet': snippet})
|
| 88 |
+
return results
|
| 89 |
+
|
| 90 |
+
# Streamlit App
|
| 91 |
+
def main():
|
| 92 |
+
st.set_page_config(page_title="Google Search Clone", layout="centered")
|
| 93 |
+
load_css()
|
| 94 |
+
|
| 95 |
+
st.markdown('<div class="search-bar">', unsafe_allow_html=True)
|
| 96 |
+
query = st.text_input("Enter your search query:", label_visibility="collapsed")
|
| 97 |
+
search_button = st.button("Search")
|
| 98 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 99 |
+
|
| 100 |
+
if search_button and query.strip():
|
| 101 |
+
with st.spinner("Searching..."):
|
| 102 |
+
html_content, captcha_detected = search_google(query)
|
| 103 |
+
|
| 104 |
+
if captcha_detected:
|
| 105 |
+
st.markdown("""
|
| 106 |
+
<div class="captcha-warning">
|
| 107 |
+
<h3>CAPTCHA Detected</h3>
|
| 108 |
+
<p>Google has detected unusual traffic from this application. Please solve the CAPTCHA manually or try again later.</p>
|
| 109 |
+
</div>
|
| 110 |
+
""", unsafe_allow_html=True)
|
| 111 |
+
return
|
| 112 |
+
|
| 113 |
+
if html_content:
|
| 114 |
+
search_results = parse_search_results(html_content)
|
| 115 |
+
for result in search_results:
|
| 116 |
+
st.markdown(f"""
|
| 117 |
+
<div class="search-result">
|
| 118 |
+
<h3><a href="{result['link']}" target="_blank">{result['title']}</a></h3>
|
| 119 |
+
<p>{result['snippet']}</p>
|
| 120 |
+
</div>
|
| 121 |
+
""", unsafe_allow_html=True)
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
main()
|