这是我的代码:
$curl = curl_init('http://www.houseoffraser.co.uk/');
$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13";
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_TIMEOUT, 400);
ini_set('max_execution_time', 300);
$page = curl_exec($curl);
if(curl_errno($curl)) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}
$html= curl_close($curl);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$regex = '/<nav class="hof-buttons">(.*?)<\/nav>/s';
if (preg_match($regex, $page, $list)) {
echo preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $list[0])."<br />";
} else {
print "Not found";
}
我试图只从div标签中获取url名称。但它只给我错误。我希望主要有这样的东西:
<div class="a"><a href="abc.php">a linki</a></div>
在代码中必须是这样的:
if ( preg_match($regex, $page, $list) ){};
echo <a href="$list[1]"> $list[0]</a>;
但是当我使用它时,它会给我错误或没有数组。我希望有这样的代码,但是如何在preg_match中添加我想要的内容,或者如何调用div中的链接?
答案 0 :(得分:1)
好的,这是整个解决方案(如果这是您正在寻找的)。
而且,顺便说一下,没有 curl ,只需 file_get_contents()做到了:
我接管了你的三步法:
<?php
$page = file_get_contents('http://www.houseoffraser.co.uk/');
if($page===false) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}
if ( preg_match_all('%<nav class=[\'"]{1,1}hof-buttons-set left[\'"]{1,1}>(.*?)</nav>%si', $page, $regs1, PREG_PATTERN_ORDER) ) {
for ($x1 = 0; $x1 < count($regs1[0]); $x1++) {
if ( preg_match_all('%<div.*?<a href=[\'"]{1,1}([^\'"]*?)[\'"]{1,1}>(.*?)</a>.*?</div>%sim', $regs1[1][$x1], $regs2, PREG_PATTERN_ORDER) ) {
for ($x2 = 0; $x2 < count($regs2[0]); $x2++) {
$link = $regs2[1][$x2];
if (preg_match('/<img.*? title=[\'"]{1,1}(.*?)[\'"]{1,1}/sim', $regs2[2][$x2], $regs3)) {
// No text, but image with title
$text = $regs3[1];
} elseif (preg_match('%<span.*?class=[\'"]{1,1}hof-label[\'"]{1,1}.*?>(.*?)</span>%sim', $regs2[2][$x2], $regs3)) {
// Text in <span class="hof-label">...</span>
$text = $regs3[1];
} else {
// Plain text
$text = $regs2[2][$x2];
}
echo '<a href="'.$link.'" title="'.$link.'" target="_blank">' . trim($text) . '</a><br />';
}
} else {
echo '<span style="color:red; font-weight:bold;">HREF not found<span><br />';
}
}
} else {
echo '<span style="color:red; font-weight:bold;">NAV not found<span><br />';
exit;
}
?>
文字:女性链接:http://www.houseoffraser.co.uk/Women%27s+Designer+Clothing/03,default,sc.html
文字:连衣裙链接:http://www.houseoffraser.co.uk/women%27s+designer+dresses/301,default,sc.html
[....]