如何使用php从此链接中提取帖子图像?
我读到我不能用正则表达式做这件事。
非常感谢。
发布于 2013-07-03 18:22:18
$content=file_get_contents($url);
if (preg_match("/<img.*src=\"(.*)\".*class=\".*pinit\".*>/", $content, $matches))
{
echo "Match was found <br />";
echo $matches[0];
}$matches将打印整个图像标记。如果您只想提取网址,那么您可以使用$matches1来获得相同的:)
发布于 2013-07-03 18:26:25
你可以也必须用DOM解析你的html,下面是你的例子:
$curlResource = curl_init('http://www.huffingtonpost.it/2013/07/03/stupri-piazza-tahrir-durante-proteste-anti-morsi_n_3538921.html?utm_hp_ref=italy');
curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curlResource, CURLOPT_AUTOREFERER, true);
$page = curl_exec($curlResource);
curl_close($curlResource);
$domDocument = new DOMDocument();
$domDocument->loadHTML($page);
$xpath = new DOMXPath($domDocument);
$urlXpath = $xpath->query("//img[@id='img_caption_3538921']/@src");
$url = $urlXpath->item(0)->nodeValue;
echo $url;花点时间学习一些DOM和XPATH,这是值得的。
发布于 2013-07-03 20:56:17
试试这个。
$content=file_get_contents($url);
if (preg_match("/src=[\"\'][^\'\']+[\"\']/", $content, $matches))
{
echo "Match was found <br />";
echo $matches[0];
}https://stackoverflow.com/questions/17445111
复制相似问题