Answer the question
In order to leave comments, you need to log in
Why is only the last link parsed?
Why does this code only parse the last link that is present in the file links.txt
?
Внутри содержимого данного файла - ссылки, по одной на каждую строку.
<?php
require_once 'phpQuery/phpQuery.php';
// Настройки парсинга
$product_url = file("links.txt");
//ПРОЧЕЕ
function request($id, $timeout = 10, $proxy = false) {
$headers[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:61.0) Gecko/20100101 Firefox/61.0";
$headers[] = "Accept: */*";
$headers[] = "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result['httpcode'] = $httpcode;
$result['data'] = $data;
return $result;
}
foreach($product_url as $id) {
$max_timout = 10;
$proxy = false;
$data = request($id, $max_timout, $proxy);
$pq = phpQuery::newDocument($data['data']);
$linkslist = (trim($id)."\n");
// Заголовок продукта
$result['title'] = trim($pq->find('div.range-revamp-header-section__title--big')->html());
print_r($result['title']);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question