File size: 5,126 Bytes
7312ff0
d803a0c
7312ff0
74ee06b
 
504d0ce
c9aae36
74ee06b
0246891
79a418e
 
 
 
 
06c02bc
 
 
 
79a418e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c169589
63e4240
c5911fa
 
5bb3dc8
c5911fa
 
63e4240
3d1a42b
c5911fa
3d1a42b
 
 
c5911fa
 
 
 
 
 
 
 
 
 
 
 
 
a4601fc
 
 
 
 
 
74ee06b
58c22b3
 
 
 
b481b5e
7e0ed7b
 
 
 
 
 
 
 
 
af4b9a6
7e0ed7b
 
0246891
7e0ed7b
 
9c7a056
 
2997260
 
7e0ed7b
 
9c7a056
7e0ed7b
 
79a418e
0246891
 
 
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
125
126
127
128
129
130
131
132
133
import requests
import gradio as gr
from bs4 import BeautifulSoup
from rich import print
from urllib.parse import urlparse
import re
from googlesearch import search
from urllib.parse import parse_qs

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time


opt = Options()
opt.add_argument('--headless')
driver = webdriver.Chrome(options=opt)

def google_search_with_time_filter(query, start_date, end_date):
    # Mở trang Google
    driver.get("https://www.google.com")

    # Tìm thanh tìm kiếm và nhập từ khóa
    search_box = driver.find_element(By.NAME, "q")
    search_box.send_keys(query)
    search_box.send_keys(Keys.RETURN)

    # Nhấn vào nút "Công cụ"
    tools_button = driver.find_element(By.XPATH, "//div[@aria-label='Công cụ']")
    tools_button.click()

    # Nhấn vào nút "Bất kỳ lúc nào"
    any_time_button = driver.find_element(By.XPATH, "//div[text()='Bất kỳ lúc nào']")
    any_time_button.click()

    # Chọn phạm vi thời gian
    custom_range_button = driver.find_element(By.XPATH, "//li//span[text()='Phạm vi tùy chỉnh']")
    custom_range_button.click()

    # Nhập ngày bắt đầu và ngày kết thúc
    start_date_input = driver.find_element(By.XPATH, "//input[@aria-label='Ngày bắt đầu']")
    end_date_input = driver.find_element(By.XPATH, "//input[@aria-label='Ngày kết thúc']")
    start_date_input.send_keys(start_date)
    end_date_input.send_keys(end_date)
    end_date_input.send_keys(Keys.RETURN)

    # Đợi trang tải kết quả
    time.sleep(2)

    # Lấy các kết quả
    results = driver.find_elements(By.CLASS_NAME, "tF2Cxc")

    # Hiển thị kết quả
    for result in results[:5]:  # Lấy 5 kết quả đầu tiên
        title = result.find_element(By.TAG_NAME, "h3").text
        link = result.find_element(By.TAG_NAME, "a").get_attribute("href")
        print(f"Title: {title}")
        print(f"Link: {link}")
        print('-' * 80)

# Thực hiện tìm kiếm với bộ lọc thời gian
google_search_with_time_filter("Python programming", "01/01/2023", "12/31/2023")

# Đóng trình duyệt sau khi hoàn tất
driver.quit()

def run_lora(prompt,site,start,end):
    url = ("https://www.google.com/search?q=inurl:" +site + " " +prompt + "&tbs=cdr%3A1%2Ccd_min%3A"+start+"%2Ccd_max%3A" + end)
    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'}
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.content)
    # Tìm tất cả các liên kết trong kết quả tìm kiếm
    results = soup.find_all('a')
    print(results)
    mLink=""
    # Lọc và hiển thị các liên kết
    for link in  soup.find_all("a",href=re.compile("(?<=/url\?q=)(htt.*://.*)")):
        print (re.split(":(?=http)",link["href"].replace("/url?q=","")))
        mLink+=str(re.split(":(?=http)",link["href"].replace("/url?q=","").replace("'","")))
    
    # Lọc theo ngày tháng (nếu ngày tháng được cung cấp trong kết quả)
    date_elements = soup.find_all('span', class_='f')
    for date_elem in date_elements:
        date_text = date_elem.text
        try:
            # Kiểm tra định dạng ngày tháng và so sánh
            date = datetime.strptime(date_text, '%m-%d-%Y')  # Ví dụ: '23 August 2024'
            if start <= date.strftime('%m-%d-%Y') <= end:
                print(f"Date: {date_text}")
        except ValueError:
            # Bỏ qua các định dạng ngày tháng không thể phân tích
            pass
def extract_href(href):
    url = urlparse(href)
    query = parse_qs(url.query)
    if not ('q' in query and query['q'] and len(query['q']) > 0):
        return None
    return query['q'][0]
    
def make_url(query,start,end):
    return f"https://www.google.com/search?q={query}&rlz=1C1CHBF_enUS1024US1025&biw=1564&bih=932&sxsrf=ALiCzsaGPneyPAo-kyllnxBBtXe-FGWorQ%3A1665448856808&source=lnt&tbs=sbd%3A1%2Ccdr%3A1%2Ccd_min%3A{start}%2Ccd_max%3A{end}&tbm=nws"

    
with gr.Blocks() as app:
    gr.HTML("""<html>
  <head>
    <style>
      h1 {
        text-align: center;
      }
    </style>
  </head>
  <body>
    <h1>Get survey</h1>
  </body>
</html>""")
    with gr.Row():
        with gr.Column():
            
            input_textbox = gr.Textbox(lines=5, placeholder="Enter key", label="Input Text")
            input_sitebox = gr.Textbox(lines=5, placeholder="Enter site", label="Site Text")
            start = gr.Textbox(lines=5, placeholder="Enter start", label="Enter start")
            end = gr.Textbox(lines=5, placeholder="Enter End", label="Enter End")
        with gr.Column():
            
            translated_textbox = gr.Textbox(lines=5, placeholder="", label="Result Text")
    info_label = gr.HTML("")
    btn = gr.Button("GetNow")
    btn.click(google_search_with_time_filter, inputs=["site:" + input_sitebox +" " + input_textbox,start,end],outputs=[translated_textbox])

app.queue()
app.launch()