Spaces:
Runtime error
Runtime error
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 | |
def run_lora(prompt,site,start,end): | |
url = (f"https://www.google.com/search?q=site:{site}+{prompt}" | |
f"&tbs=cdr:1,cd_min:{start},cd_max:{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') | |
# Lọc và hiển thị các liên kết | |
for link in results: | |
href = link.get('href') | |
if href and href.startswith('/url?q='): | |
# Loại bỏ prefix '/url?q=' và tham số URL không cần thiết | |
full_url = href.split('/url?q=')[1].split('&')[0] | |
print(full_url) | |
# 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(run_lora, inputs=[input_textbox,input_sitebox,start,end],outputs=[translated_textbox]) | |
app.queue() | |
app.launch() |