parser php - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of parser php.php

  1. <?phpimport requests
  2. from bs4 import BeautifulSoup
  3.  
  4. def parse_url(url):
  5.     # Отправляем GET-запрос
  6.     response = requests.get(url)
  7.  
  8.     # Проверяем успешность запроса
  9.     if response.status_code == 200:
  10.         # Инициализируем объект BeautifulSoup для парсинга HTML
  11.         soup = BeautifulSoup(response.text, 'html.parser')
  12.  
  13.         # Извлекаем ссылки
  14.         links = [link.get('href') for link in soup.find_all('a')]
  15.  
  16.         # Извлекаем заголовки
  17.         headers = [header.get_text() for header in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])]
  18.  
  19.         # Извлекаем информацию из таблиц
  20.         tables_data = []
  21.         tables = soup.find_all('table')
  22.         for table in tables:
  23.             table_data = []
  24.             rows = table.find_all('tr')
  25.             for row in rows:
  26.                 row_data = [cell.get_text() for cell in row.find_all(['th', 'td'])]
  27.                 table_data.append(row_data)
  28.             tables_data.append(table_data)
  29.  
  30.         return {
  31.             "links": links,
  32.             "headers": headers,
  33.             "tables_data": tables_data
  34.         }
  35.     else:
  36.         print("Ошибка при запросе:", response.status_code)
  37.         return None
  38.  
  39. # Пример использования
  40. url = "https://example.com"
  41. data = parse_url(url)
  42. if data:
  43.     print("Ссылки:")
  44.     print(data["links"])
  45.     print("\nЗаголовки:")
  46.     print(data["headers"])
  47.     print("\nДанные из таблиц:")
  48.     print(data["tables_data"])
  49.  
  50.  
File Description
  • parser php
  • PHP Code
  • 15 Feb-2024
  • 1.58 Kb
You can Share it: