目前我有一個 curl 腳本可以從這個 url 中抓取信息。$url = 'https://www.marktplaats.nl' . '/q/iphone/p/1/#offeredSince:Gisteren/';是否可以從更多鏈接中自動抓取信息?例如,當(dāng)我將一些變量設(shè)置為 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];我想要一個“選項”,讓我通過為頁碼設(shè)置可變值來抓取多個鏈接。在我弄清楚之前編輯:這是我目前的代碼。我是否需要為此使用 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,為我提供了正確的循環(huán)!
1 回答

qq_笑_17
TA貢獻(xiàn)1818條經(jīng)驗 獲得超7個贊
一個簡單的for loop應(yīng)該工作:
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 關(guān)注
- 219 瀏覽
添加回答
舉報
0/150
提交
取消