我需要從網頁上顯示一些消息,所以我需要從網站提取數據。但我無法提取數據,如下面的代碼:如何從網頁上抓取數據?
$html=file_get_html("http://listverse.com/2014/12/01/10-times-us-foreign-policy-was-wildly-inconsistent/");
foreach($html->find('article h2') as $element)
{
echo "<h2>".$element->plaintext."</h2>"."<br>";
foreach ($html->find('article h2 p') as $element1) {
echo "<pre>";print_r($element1->plaintext);
}
但我得到正確的標題,但每個段落是多餘的。
這實際上是一個骯髒的方法從該新聞網站獲取數據。當您查看源代碼時,您會看到有一個.rss提要。你爲什麼不用它來獲取數據? 例如:http://listverse.com/feed/ 而您的帖子特定的一個: http://listverse.com/2014/12/01/10-times-us-foreign-policy-was -wildly-inconsistent/feed/ – Duikboot 2014-12-02 12:50:54
rss沒有完整的詳細信息 – kavita 2014-12-02 12:58:53
請注意,複製另一個網站的新聞並重新發布它可能會違反版權。 – Quentin 2014-12-02 13:00:18