0

您好,我使用 preg_match_all 函数来抓取页面上的内容,但是当我尝试抓取一些特定部分(如细节部分)时,它会向我发送一个数组!

该页面上的代码结构是

<div class="f slp">DETAILS I WANT TO GET</div>

以前为了获取网址 n 标题,我使用了类似的代码

//so this gets URLs in href=""
preg_match_all('/a href="([^"]+)" class=l.+?>.+?<\/a>/',$scraped,$results);

但这次我想在该页面的结构下获取一些细节

<div class="f slp">DETAILS I WANT TO GET</div>
4

2 回答 2

1
preg_match_all("#<div class=\"f slp\">(.*?)<\/div>#si", $source, $match);

foreach($match[1] as $val) {
    echo $val."<br>";
}
于 2012-05-05T02:07:14.120 回答
1

请查看PHP Simple HTML DOM Parser一个非常易于使用的库,它可以很容易地从 html 中提取内容。

// from the documentation
$html = str_get_html("<div>foo <b>bar</b></div>");
$e = $html->find("div", 0);
echo $e->tag; // Returns: " div"
echo $e->outertext; // Returns: " <div>foo <b>bar</b></div>"
echo $e->innertext; // Returns: " foo <b>bar</b>"
echo $e->plaintext; // Returns: " foo bar"

阅读更多手册

于 2012-05-05T02:21:39.480 回答