>  기사  >  백엔드 개발  >  웹 스크레이핑 방법

웹 스크레이핑 방법

Linda Hamilton
Linda Hamilton원래의
2024-10-17 19:08:02278검색

How to Web Scrape

Web Scraping with PHP

Question:

How can I extract the title, an image, and text or description from a specified URL without external libraries in PHP?

Answer:

To simplify this task, consider utilizing the simple_html_dom library. The following example demonstrates how to obtain the title and first image using this library:

<code class="php">require 'simple_html_dom.php';

$html = file_get_html('http://www.google.com/');
$title = $html->find('title', 0);
$image = $html->find('img', 0);

echo $title->plaintext . "<br>\n";
echo $image->src;</code>

If you prefer to avoid external libraries, you can extract data using regular expressions, though this approach is not recommended for HTML.

<code class="php">$data = file_get_contents('http://www.google.com/');

preg_match('/<title>([^<]+)<\/title>/i', $data, $matches);
$title = $matches[1];

preg_match('/<img[^>]*src=["\']([^\'"']+)["\'][^>]*>/i', $data, $matches);
$img = $matches[1];

echo $title . "<br>\n";
echo $img;</code>

위 내용은 웹 스크레이핑 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.