我不能仅使用 preg_match 或 preg_replace 调用 div 中的链接
I cannot call only links in the div with preg_match or preg_replace
这是我的代码:
$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 中的链接?
好的,这是完整的解决方案(如果这就是您要找的)。
而且,顺便说一句,没有 curl,只是 file_get_contents() 做到了:
我接手了你的三步法:
- 第 1 步:在 之间提取。
- 第 2 步:在 ... 之间提取所有 hrefs.
- 第 3 步:从不同来源收集文本并进行清理。
代码
<?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;
}
?>
结果
文字:女性
link:http://www.houseoffraser.co.uk/Women%27s+Designer+Clothing/03,default,sc.html
文本:连衣裙
link:http://www.houseoffraser.co.uk/women%27s+designer+dresses/301,default,sc.html
[....]
这是我的代码:
$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 中的链接?
好的,这是完整的解决方案(如果这就是您要找的)。
而且,顺便说一句,没有 curl,只是 file_get_contents() 做到了:
我接手了你的三步法:
- 第 1 步:在 之间提取。
- 第 2 步:在 ...之间提取所有 hrefs.
- 第 3 步:从不同来源收集文本并进行清理。
代码
<?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;
}
?>
结果
文字:女性
link:http://www.houseoffraser.co.uk/Women%27s+Designer+Clothing/03,default,sc.html
文本:连衣裙
link:http://www.houseoffraser.co.uk/women%27s+designer+dresses/301,default,sc.html
[....]