【发布时间】:2017-02-19 23:31:54
【问题描述】:
简介
我正在尝试抓取特定网站的标题和相关链接。然后抓取链接页面以获取文章文本。我正在使用 cURL 和 simple_html_dom.php。
问题
我正在尝试整理所有这些数据;标题、链接和文章文本,位于具有以下结构的多维数组中:
Array
(
[0] => Array
(
[0] => title 1
[1] => link 1
[2] => text 1
)
[1] => Array
(
[0] => title 2
[1] => link 2
[2] => text 2
)
)
但无论我如何尝试实现这一点,结构都是完全错误和不正确的。如何保存相应的文本及其链接和标题?
代码
<?php
$results_page = curl($url); // Downloading the results page using curl() funtion
$html = new simple_html_dom();
$html->load($results_page);
$items = $html->find('h2[class=artTitle]'); // Exploding each h2
foreach($items as $post) {
$headlines[] = array($post->children(0)->innertext); // Saving h2 text
$url_results[] = ($post->children(0)->href); // Saving h2 link
}
foreach($url_results as $url_result) {
$results_page = curl($url_result);
$html->load($results_page);
foreach($html->find('#articleText p[!class]') as $post) // Finding all p elements inside container
$articles[] = array($post->plaintext); // Adding p elements to array
}
?>
我已经排除了我的 cURL 函数,因为我不认为它是这个问题的一个因素,也不想让问题变得混乱。如果需要,我当然会添加它。
以下是我目前的数组结构:
$headlines 的当前数组结构:
Array
(
[0] => Array
(
[0] => Headline 1¨
)
[1] => Array
(
[0] => Headline 2¨
)
)
$url_results 的当前数组结构:
Array
(
[0] => Link 1
[1] => Link 2
)
Array
$articles 的当前数组结构:
Array
(
[0] => Array
(
[0] => Paragraph 1 Text 1
)
[1] => Array
(
[0] => Paragraph 2 Text 1
)
[2] => Array
(
[0] => Paragraph 1 Text 2
)
)
【问题讨论】:
-
我在您的示例中看到了您的预期输出,但我没有看到当前输出。你也可以提供吗?
-
数组结构现已添加,谢谢提醒。
标签: php arrays curl multidimensional-array web-scraping