(.*)

Как получить контент сайта на PHP

Парсер контента на языке PHP – это важный инструмент для веб-разработчиков, которые работают с различными источниками данных. Он позволяет извлекать нужную информацию из HTML-страниц, XML-файлов и других форматов, а также обрабатывать ее в соответствии с заданными правилами.

Одним из основных преимуществ парсера контента является возможность автоматизировать процесс получения и обработки данных, что позволяет сократить время выполнения задач и уменьшить вероятность ошибок.

Для получения контента определённой страницы сайта есть простое решение с помощью собственной функции php — file_get_contents . Всё, что требуется это передать в функцию URL нужной страницы.

Получение контента с помощью библиотеки SimpleHTMLDOM

Для более качественной работы функции лучше воспользоваться подключаемой библиотекой SimpleHTMLDOM. В simplehtmldom есть методы для удаленной загрузки страниц. После подключения файла библиотеки, нам доступны 2 функции для обработки HTML строк:

str_get_html(str) и file_get_html(url)

Они делают одно и тоже, преобразуют HTML текст в DOM дерево, различаются лишь источники.

str_get_htm – на вход получает обычную строку, т.е. если вы получили HTML прибегнув к curl, или file_get_contents то вы просто передаете полученный текст этой функции.

$html = str_get_html(‘html код’);

file_get_html – сама умеет загружать данные с удаленного URL или из локального файла

К сожалению, file_get_html загружает страницы обычным file_get_contents . Это значит если хостер, выставил в php.ini allow_url_fopen = false (т.е. запретил удаленно открывать файлы), то загрузить что-то удаленно, не получится. Да и серьезные веб сайты таким способом парсить не стоит, лучше использовать CURL с поддержкой proxy и ssl .

$html = file_get_html(‘http://www.yandex.ru/’);
в результате, в переменной $html будет объект типа simple_html_dom.

При больших объемах данных, в библиотеке происходит утечка памяти. Поэтому после окончания одного цикла надо ее чистить.

Делает это метод clear .

К примеру грузим 5 раз сайт www.yandex.ru с разными поисковыми запросами

include 'simple_html_dom.php'; $k = 5; while($k>0)< $html = file_get_html('http://yandex.ru/yandsearch?text=hi'.$k.'&lr=11114'); // загружаем данные // как-то их обрабатываем $html->clear(); // подчищаем за собой unset($html); $k--; >

Ниже приведен ещё один пример использования библиотеки Simple HTML DOM Parser для парсинга HTML-страницы и извлечения заголовков новостей:

// Подключаем библиотеку require_once('simple_html_dom.php'); // Получаем содержимое страницы $html = file_get_html('http://example.com/news.html'); // Ищем все заголовки новостей foreach($html->find('h2.news-title') as $title) < // Выводим текст заголовка echo $title->plaintext; >

В этом примере мы используем библиотеку Simple HTML DOM Parser , которая предоставляет простой и удобный API для работы с HTML-документами. Сначала мы получаем содержимое страницы с помощью функции file_get_html() , затем находим все элементы с тегом h2 и классом news-title с помощью метода find() . Наконец, мы выводим текст каждого заголовка с помощью свойства plaintext .

Получение контента с помощью cURL

Неоспоримыми преимуществами в функционале пользуется библиотека или можно сказать модуль PHP — cURL . Для полноценного контролируемого получения контента здесь есть множество разных доплнений. Это и практически полноценный эмулятор браузерного обращения к сайту, работа скрипта через proxy с приватной идентификацией и многое другое. Ниже показана функция получения контента с помощью cURL .

function curl($url, $postdata='', $cookie='', $proxy='') < $uagent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16"; $ch = curl_init( $url ); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // возвращает веб-страницу curl_setopt($ch, CURLOPT_HEADER, 0); // возвращает заголовки @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // переходит по редиректам curl_setopt($ch, CURLOPT_ENCODING, ""); // обрабатывает все кодировки curl_setopt($ch, CURLOPT_USERAGENT, $uagent); // useragent curl_setopt($ch, CURLOPT_TIMEOUT, 10); // таймаут ответа //curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // останавливаться после 10-ого редиректа if($proxy !='')if($mass_proxy[2]!="" and $mass_proxy[3]!="")< curl_setopt($ch, CURLOPT_PROXYUSERPWD,$mass_proxy[2].':'.$mass_proxy[3]);>// если необходимо предоставить имя пользователя и пароль 'user:pass' if(!empty($postdata)) < curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); >if(!empty($cookie)) < //curl_setopt($ch, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'].'/2.txt'); //curl_setopt($ch, CURLOPT_COOKIEFILE,$_SERVER['DOCUMENT_ROOT'].'/2.txt'); >$content = curl_exec( $ch ); $err = curl_errno( $ch ); $errmsg = curl_error( $ch ); $header = curl_getinfo( $ch ); curl_close( $ch ); $header['errno'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $content; return $header['content']; >

Продвинутый скрипт получения контента на PHP

Ещё один способ получения контента встроенными функциями php я нашёл на просторах интернета. Пока не использовал, но привожу здесь для полноты картины, так как это решение тоже заслуживает достойного внимания. Далее, как в ©Источнике:
Полезными качествами, в данном контексте, будут возможность получения множества атрибутов запрашиваемого контента, а также возможность получения заголовка ответа сервера и времени выполнения запроса. Данная функция использует встроенные в PHP функции для работы с сокетами, которые предназначены для соединения клиента с сервером.

; $URL_SCHEME = ( isset($URL_PARTS['scheme']))?$URL_PARTS['scheme']:'http'; $URL_HOST = ( isset($URL_PARTS['host']))?$URL_PARTS['host']:''; $URL_PATH = ( isset($URL_PARTS['path']))?$URL_PARTS['path']:'/'; $URL_PORT = ( isset($URL_PARTS['port']))?intval($URL_PARTS['port']):80; if( isset($URL_PARTS['query']) && $URL_PARTS['query']!='' ) < $URL_PATH .= '?'.$URL_PARTS['query']; >; $URL_PORT_REQUEST = ( $URL_PORT == 80 )?'':":$URL_PORT"; //--- build GET request --- $USER_AGENT = ( $user_agent == null )?'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)':strval($user_agent); $GET_REQUEST = "GET $URL_PATH HTTP/1.0 " ."Host: $URL_HOST$URL_PORT_REQUEST " ."Accept: text/plain " ."Accept-Encoding: identity " ."User-Agent: $USER_AGENT "; //--- open socket --- $SOCKET_TIME_OUT = 30; $SOCKET = @fsockopen($URL_HOST, $URL_PORT, $ERROR_NO, $ERROR_STR, $SOCKET_TIME_OUT); if( $SOCKET ) < if( fputs($SOCKET, $GET_REQUEST)) < socket_set_timeout($SOCKET, $SOCKET_TIME_OUT); //--- read header --- $header = ''; $SOCKET_STATUS = socket_get_status($SOCKET); while( !feof($SOCKET) && !$SOCKET_STATUS['timed_out'] ) < $temp = fgets($SOCKET, 128); if( trim($temp) == '' ) break; $header .= $temp; $SOCKET_STATUS = socket_get_status($SOCKET); >; //--- get server code --- if( preg_match('~HTTP/(d+.d+)s+(d+)s+(.*)s* ~si', $header, $res)) $SERVER_CODE = $res[2]; else break; if( $SERVER_CODE == ABI_URL_STATUS_OK ) < //--- read content --- $content = ''; $SOCKET_STATUS = socket_get_status($SOCKET); while( !feof($SOCKET) && !$SOCKET_STATUS['timed_out'] ) < $content .= fgets($SOCKET, 1024*8); $SOCKET_STATUS = socket_get_status($SOCKET); >; //--- time results --- $TIME_END = explode(' ', microtime()); $TIME_TOTAL = ($TIME_END[0]+$TIME_END[1])-($TIME_START[0]+$TIME_START[1]); //--- output --- $URL_RESULT['header'] = $header; $URL_RESULT['content'] = $content; $URL_RESULT['time'] = $TIME_TOTAL; $URL_RESULT['description'] = ''; $URL_RESULT['keywords'] = ''; //--- title --- $URL_RESULT['title'] =( preg_match('~~U', $content, $res))?strval($res[1]):''; //--- meta tags --- if( preg_match_all('~]+>~', $content, $res, PREG_SET_ORDER) > 0 ) < foreach($res as $meta) $URL_RESULT[strtolower($meta[1])] = $meta[2]; >; > elseif( $SERVER_CODE == ABI_URL_STATUS_REDIRECT_301 || $SERVER_CODE == ABI_URL_STATUS_REDIRECT_302 ) < if( preg_match('~location:s*(.*?) ~si', $header, $res)) < $REDIRECT_URL = rtrim($res[1]); $URL_PARTS = @parse_url($REDIRECT_URL); if( isset($URL_PARTS['scheme'])&& isset($URL_PARTS['host'])) $url = $REDIRECT_URL; else $url = $URL_SCHEME.'://'.$URL_HOST.'/'.ltrim($REDIRECT_URL, '/'); >else < break; >; >; >;// GET request is OK fclose($SOCKET); >// socket open is OK else < break; >; $TRY_ID++; > while( $TRY_ID ; ?>

Итак, входящими параметрами являются: $url — строка, содержащая URL http-протокола, $user_agent — строка с любым юзер-агентом (если пропустить параметр или установить его в null — user_agent будет как в IE). Константа MAX_REDIRECTS_NUM устанавливает количество разрешенных редиректов (поддерживаются 301 и 302 редиректы).

Теперь перейдем к примерам практического использования этой функции:

 else print 'Запрашиваемая страница недоступна.'; ?>

Как видно из вышеприведенного примера, мы можем получить всю информацию по запрошенному URL . Кроме того, можно получить значения любого мета-тега. Для этого можно воспользоваться следующим кодом:

Парсер контента на языке PHP – это важный инструмент для получения и обработки данных из различных источников. Благодаря мощным библиотекам и инструментам, разработчики могут легко и удобно извлекать нужную информацию из HTML-страниц, XML-файлов и других форматов.

Дата публикации: 2019-04-26

Источник

How to Get an HTML Page from a URL in Python?

Be on the Right Side of Change

This tutorial shows you how to perform simple HTTP get requests to get an HTML page from a given URL in Python!

Problem Formulation

Given a URL as a string. How to extract the HTML from the given URL and store the result in a Python string variable?

Example: Say, you want to accomplish the following:

url = 'https://google.com' # . Code to extract HTML page here . print(result) # . Google HTML file: ''' . '''

Let’s study the four most important methods to access a website in your Python script!

Method 1: requests.get(url)

The simplest solution is the following:

import requests print(requests.get(url = 'https://google.com').text)
  • Import the Python library requests that handles the details of requesting the websites from the server in an easy-to-process format.
  • Use the requests.get(. ) method to access the website and pass the URL ‘https://google.com’ as an argument so that the function knows which location to access.
  • Access the actual body of the get request (the return value is a request object that also contains some useful meta information like the file type, etc.).
  • Print the result to the shell.

The output is the desired Google website:

Note that you may have to install the requests library with the following command in your operating system terminal:

Method 2: One-Liner with requests.get()

Sometimes you don’t want to open an interactive Python session to access the URL. No problem, you can make the previous solution a one-liner and run it from your operating system command line or terminal.

Note that the semicolon is used to one-linerize the previously discussed method. This is useful if you want to run this command from your operating system with the following command:

python -r "import requests; print(requests.get(url = 'https://google.com').text)"

The output, again, is the desired Google HTML page:

Method 3: urllib.request

A recommended way to fetch web resources from a website is the urllib.request() function. This also works to create a simple one-liner to access the Google website in Python 3 as before:

import urllib.request as r page = r.urlopen('https://google.com') print(page.read())

Again, you return a Request object that can be accessed to read the server’s response.

Note that this reads the file as a byte string. If you want to read the HTML file as a string, you need to convert the result using Python’s decode() method:

import urllib.request as r page = r.urlopen('https://google.com') print(page.read().decode('utf8'))

Here’s the output of this code snippet with most of the HTML content omitted for brevity.

Method 4: One-Liner with urllib.request

You can also cram everything into a single line so that you can run it from your OS’s terminal:

python -r "import urllib.request as r; print(r.urlopen('https://google.com').read())"

Try It Yourself

You can try Methods 1 and 3 yourself in our interactive Juypter notebook with your own desired website URL:

How to Get an HTML Page from a URL in Python? Interactive Shell

To boost your skills in Python, feel free to check out the world’s most comprehensive Python email academy and download your Python cheat sheets here:

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

Be on the Right Side of Change 🚀

  • The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. 🤖
  • Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
  • Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.

Learning Resources 🧑‍💻

⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!

Join the Finxter Academy and unlock access to premium courses 👑 to certify your skills in exponential technologies and programming.

New Finxter Tutorials:

Finxter Categories:

Источник

Читайте также:  Test
Оцените статью