0

我写了这个脚本,它可以工作,但只抓取页面上的第一个链接,然后停止:

<?php
$handle = fopen("localurls.csv","r");
while(($line=fgetcsv($handle))!==FALSE) {

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $line[0],
));
$resp = curl_exec($curl);
curl_close($curl);
$regex_body = '%<a href="(.+?)">%s';
$myBody = preg_match($regex_body, $resp, $matches);
$myFile = "localdownload.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $matches[0]);
fclose($fh);
sleep(2);
}
?>

.csv 文件是我有脚本爬网的页面列表。当我运行它时,我得到每个页面上的第一个链接.. IE

<a href="www.google.com">

然后我将它写入一个txt文件。有谁知道修改它以继续在页面上查看代码之间的所有迭代的方法

<a href=" 

">

想了想,在网上找了下怎么实现的,但是没有用。

4

1 回答 1

1

您需要使用preg_match_all而不是preg_match. preg_match返回第一次出现,preg_match_all执行全局正则表达式并搜索所有实例。

http://www.php.net/manual/en/function.preg-match-all.php

于 2013-03-16T18:15:09.167 回答