目前我有一个 curl 脚本可以从这个 url 中抓取信息。$url = 'https://www.marktplaats.nl' . '/q/iphone/p/1/#offeredSince:Gisteren/';是否可以从更多链接中自动抓取信息?例如,当我将一些变量设置为 5 等时,还要同时抓取第 2、3、4、5 页等。https://www.marktplaats.nl/q/iphone/p/1/#offeredSince:Gisteren/https://www.marktplaats.nl/q/iphone/p/2/#offeredSince:Gisteren/https://www.marktplaats.nl/q/iphone/p/3/#offeredSince:Gisteren/https://www.marktplaats.nl/q/iphone/p/4/#offeredSince:Gisteren/https://www.marktplaats.nl/q/iphone/p/5/#offeredSince:Gisteren/我的 cURL 脚本支持抓取 1 个 url。但不是多个。declare(strict_types = 1);set_time_limit(0);ob_start();include 'functions.php';$curl = curl_init();$url = 'https://www.marktplaats.nl' . '/q/iphone/p/1/#offeredSince:Gisteren/';curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);$result = curl_exec($curl);$advertisements = array();preg_match_all('\/a\/.*?.*?.html', $result, $links);$advertisements = $links[0];我想要一个“选项”,让我通过为页码设置可变值来抓取多个链接。在我弄清楚之前编辑:这是我目前的代码。我是否需要为此使用 curl_multi_init,然后它是如何工作的?<?php declare(strict_types = 1);set_time_limit(0);ob_start();include 'functions.php';$curl = curl_init();$url = 'https://www.marktplaats.nl' . '/q/laptoptas/p/18/#offeredSince:Gisteren/';curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);$result = curl_exec($curl);$advertisements = array();// regex for advertisement linkspreg_match_all('%\/a\/.*?.*?.html%', $result, $links);$advertisements = $links[0];// encode the array into a JSON string$encodedString = json_encode($advertisements, JSON_PRETTY_PRINT);$decodedArray = json_decode($encodedString, true);$decodedArray = array_values(array_unique($decodedArray, SORT_REGULAR));$content = null;foreach($decodedArray as $link) { $content .= "https://marktplaats.nl" . $link . PHP_EOL;}file_put_contents('advertisements.txt', $content, FILE_APPEND);$bestand = file('advertisements.txt');$bestand = array_unique($bestand);file_put_contents('advertisements.txt', $bestand);在我想通之后编辑:好吧,正如你所读到的。我想到了。感谢 l'L'l,为我提供了正确的循环!
1 回答
qq_笑_17
TA贡献1818条经验 获得超7个赞
一个简单的for loop应该工作:
for ($i = 1; $i <= 5; $i++) {
$url = 'https://www.marktplaats.nl' . '/q/iphone/p/' . $i . '/#offeredSince:Gisteren/';
...
}
↳ https://www.php.net/manual/en/control-structures.for.php
- 1 回答
- 0 关注
- 191 浏览
添加回答
举报
0/150
提交
取消