使用PHP进行网页爬取

本文介绍了如何使用PHP进行网页抓取,适合有一定PHP基础的读者。通过简单HTML DOM解析器,文章详细讲解了下载库、解压并移动文件到项目文件夹的过程,以及如何开始编写Web爬虫代码。
摘要由CSDN通过智能技术生成

在本文中,我将向您展示如何使用PHP抓取网页。 YouTube上有本教程的视频版本,网址为

https://youtu.be/Uc5mfudMTKE(如果您喜欢以视频​​格式学习)。 我个人喜欢阅读一篇文章,因为它往往会花费更少的时间,因为您可以浏览...选择哪种格式最适合您!

本文假定您对PHP和编程概念有基本的了解,并且可以访问能够运行PHP的服务器。 如果您无权访问能够运行PHP的服务器,则可以通过观看我的安装视频在Windows 10上安装WAMP。 在某种程度上,抓取涉及对网页进行反向工程,因此有助于熟悉HTML。

尽管还有其他方法可以使用PHP抓取网页,但是本文将重点介绍简单HTML DOM解析器。 我之所以选择使用此库,是因为这是我经验丰富的库,并且易于使用,且提供了出色的文档。

安装库

您需要做的第一件事是从SourceForge下载抓取库。 您可以通过转到

http://simplehtmldom.sourceforge.net/ ,然后单击“从SourceForge下载最新版本”。

从SourceForge下载库后,解压缩压缩文件夹。 然后将“ simple_html_dom.php”文件移动到将在其中构建Web爬网程序的文件夹中。

编写剪贴代码

现在您已经安装了库,您可以开始编写我们的抓取代码了。

<?php
   # This imports and gives us access to the scraping library
   include('simple_html_dom.php');
?>
现在,您可以访问抓取库了,可以使用file_get_html函数从URL创建DOM对象。
<?php
   # This imports and gives us access to the scraping library
   include('simple_html_dom.php'); 
   # Create HTML DOM object from url
   $html = file_get_html('https://google.com');
?>
然后,您可以通过调用find方法并传入要捕获的元素的标记名,从此DOM对象中提取特定元素。 如果您只想获取特定标签的单个实例,则还可以传递索引。 如果要获取标签数组,则不要传递索引。
# Create HTML DOM object from url
$html = file_get_html('https://google.com'); 
# Gets the 0th title element from the DOM object and echos it to the webpage
echo $html->find('title',0); 
# If we don't pass an index we can get an array of all the anchor elements from the DOM object
$array_of_anchors = $html->find('a'); 
# We can echo all of the anchor elements from the array above by using a simple for loop
for( $i = 0; $i < sizeof($list_of_anchors); $i++ ){
   # echo each anchor by using the $i iterator to pull the anchor in each index position
   echo $list_of_anchors[$i];
}
除了根据标签名称选择元素外,还可以根据类或ID选择元素。
$html = file_get_html('https://google.com'); 
$array_of_hidden_divs = $html->find('div[class="hidden"]'); 
$array_of_thumbnails = $html->find('img[id="thumbnail"]');
find方法返回一个DOM对象。 这意味着我们可以自己调用find方法来获取子元素。
$html = file_get_html('https://google.com'); 
$ul = $html->find('ul',0); 
$array_of_li = $ul->find('li'); 
# This is the same as above, but in a single line
$array_of_li = $html->find('ul',0)->find('li');
您可以提取某些数据,例如元素的文本,锚标记的超链接引用或图像的来源。
$html = file_get_html('https://google.com'); 
$button_text = $html->find('button',0)->plaintext; 
$anchor_href = $html->find('a',0)->href; 
$image_source = $html->find('img',0)->src;
我希望这可以帮助您满足您的PHP Web Scraping需求。 如果您需要任何澄清,请随时提出问题。 我强烈建议您阅读文档。

From: https://bytes.com/topic/php/insights/972457-web-scraping-php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值