我正在制作一个Torrent PHP爬虫,但我遇到了问题,以下是我的代码:
结果如下:
// ... the cURL codes (they're working) ...
// Contents of the Page
$contents = curl_exec($crawler->curl);
// Find the Title
$pattern = "/<title>(.*?)<\/title>/s";
preg_match($pattern, $contents, $titlematches);
echo "Title - ".$titlematches[1]."<br/>";
// Find the Category
$pattern = "/Тип<\/td><td(?>[^>]+)>((?>[^<]+))<\/td>/s";
preg_match($pattern, $contents, $categorymatches);
echo "Category - ".$categorymatches[1]."<br/>";
这是一个HTML页面(“类型”表示类别,“电影”表示电影):
<title>The Matrix</title>
<!--Some Codes Here--!>
<tr><td>Тип</td><td valign="top" align=left>Филми</td></tr>
<!--Some Codes Here--!>
结果如下:
Title - The Matrix
Notice: Undefined offset: 1 in /var/www/spider.php on line 117
标题已经显示了,但分类却没有显示,为什么呢?我尝试使用$categorymatches[0]
、$categorymatches[2]
和$categorymatches[3]
进行输出,但没有成功。
contents
不会与categorymatches
匹配。另外,注释使用-->
而不是--!>
关闭。 - Explosion Pills$contents
不包含正确的 HTML 数据。尝试在curl_exec()
后立即输出它,看看会出现什么。我使用您提供的 HTML 在本地尝试过,并且可以正常工作,完全匹配。 - entropid