File size: 4,020 Bytes
3cb2323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import requests
from bs4 import BeautifulSoup
import streamlit as st

# Custom CSS to mimic Google's interface
def load_css():
    st.markdown("""
    <style>
    body {
        font-family: Arial, sans-serif;
        background-color: #f8f9fa;
    }
    .search-bar {
        width: 50%;
        margin: 20px auto;
        display: flex;
        align-items: center;
        border: 1px solid #dfe1e5;
        border-radius: 24px;
        padding: 10px;
        background-color: #fff;
        box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
    }
    .search-bar input {
        flex: 1;
        border: none;
        outline: none;
        font-size: 16px;
        padding: 5px;
    }
    .search-bar button {
        background: none;
        border: none;
        color: #4285f4;
        font-size: 18px;
        cursor: pointer;
    }
    .search-result {
        margin: 20px auto;
        width: 80%;
        padding: 15px;
        border: 1px solid #dfe1e5;
        border-radius: 8px;
        background-color: #fff;
        box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
    }
    .search-result h3 {
        margin: 0;
        color: #1a0dab;
        font-size: 18px;
    }
    .search-result p {
        margin: 5px 0;
        color: #545454;
        font-size: 14px;
    }
    .captcha-warning {
        text-align: center;
        margin-top: 50px;
    }
    .captcha-warning h3 {
        color: #d93025;
    }
    </style>
    """, unsafe_allow_html=True)

# Function to perform a Google search
def search_google(query, proxy=None):
    headers = {
        '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'
    }
    url = f'https://www.google.com/search?q={query.replace(" ", "+")}&hl=en'
    proxies = {"http": proxy, "https": proxy} if proxy else None
    response = requests.get(url, headers=headers, proxies=proxies, timeout=10)
    if "detected unusual traffic" in response.text.lower():  # Detect CAPTCHA
        return None, True
    return response.text, False

# Function to parse Google search results
def parse_search_results(html_content):
    soup = BeautifulSoup(html_content, 'html.parser')
    results = []
    for g in soup.find_all('div', class_='tF2Cxc'):
        title = g.find('h3').text if g.find('h3') else "No title"
        link = g.find('a')['href'] if g.find('a') else "No link"
        snippet = g.find('span', class_='aCOpRe').text if g.find('span', class_='aCOpRe') else "No snippet"
        results.append({'title': title, 'link': link, 'snippet': snippet})
    return results

# Streamlit App
def main():
    st.set_page_config(page_title="Google Search Clone", layout="centered")
    load_css()
    
    st.markdown('<div class="search-bar">', unsafe_allow_html=True)
    query = st.text_input("Enter your search query:", label_visibility="collapsed")
    search_button = st.button("Search")
    st.markdown('</div>', unsafe_allow_html=True)

    if search_button and query.strip():
        with st.spinner("Searching..."):
            html_content, captcha_detected = search_google(query)
            
            if captcha_detected:
                st.markdown("""
                <div class="captcha-warning">
                    <h3>CAPTCHA Detected</h3>
                    <p>Google has detected unusual traffic from this application. Please solve the CAPTCHA manually or try again later.</p>
                </div>
                """, unsafe_allow_html=True)
                return
            
            if html_content:
                search_results = parse_search_results(html_content)
                for result in search_results:
                    st.markdown(f"""
                    <div class="search-result">
                        <h3><a href="{result['link']}" target="_blank">{result['title']}</a></h3>
                        <p>{result['snippet']}</p>
                    </div>
                    """, unsafe_allow_html=True)

if __name__ == "__main__":
    main()