Spaces:
Running
Running
| import gradio as gr | |
| import requests | |
| from bs4 import BeautifulSoup | |
| def process_url(url, method): | |
| try: | |
| if method == "Обычный запрос": | |
| response = requests.get(url) | |
| elif method == "Через прокси": | |
| proxies = { | |
| "http": "http://your_proxy:port", | |
| "https": "http://your_proxy:port" | |
| } | |
| response = requests.get(url, proxies=proxies) | |
| elif method == "Только заголовки": | |
| response = requests.head(url) | |
| return f"Заголовки:\n{response.headers}" | |
| response.raise_for_status() | |
| if method != "Только заголовки": | |
| soup = BeautifulSoup(response.content, 'html.parser') | |
| page_title = soup.title.string if soup.title else "Нет заголовка" | |
| page_preview = soup.prettify()[:500] # Ограничиваем вывод до 500 символов | |
| return f"Заголовок страницы: {page_title}\n\nПредварительный просмотр кода:\n{page_preview}" | |
| except requests.exceptions.RequestException as e: | |
| return f"Ошибка при обработке URL: {e}" | |
| # Создаем интерфейс | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## Введите ссылку для просмотра исходного кода сайта") | |
| url_input = gr.Textbox(label="Введите URL", placeholder="http://example.com") | |
| method_selector = gr.Dropdown(choices=["Обычный запрос", "Через прокси", "Только заголовки"], | |
| label="Метод обработки", value="Обычный запрос") | |
| submit_button = gr.Button("Отправить") | |
| output_text = gr.Textbox(label="Результат", lines=20) | |
| # Обработка нажатия кнопки | |
| submit_button.click(process_url, inputs=[url_input, method_selector], outputs=output_text) | |
| # Запуск приложения | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |