我正在创建一个抓取代码,抓取特定郊区的每个地址。但我陷入了这个问题;“使用未定义的常量j-假设'j'”,并在$target_url中标识了它。有人能帮我解决这个问题吗?
$arr = array("Illawong 2232",
"Strathfield 2135",
"Croydon 2132",
"Croydon Park 2133",
"Burwood 2134",
"Parramatta 2150",
"Hurtsville 2220",
"Seven Hills 2153",
"Blacktown 2148",
"Toongabie 2146",
"Winston Hills 2153",
"Bondi Beach 2026",
"Bondi Junction 2022",
"Coogee 2034",
"Pymble 2073",
"Miranda 2228",
"Caringbah 2229",
"Sylvania 2224",
"Drummoyne 2047",
"Concord 2137"
);
$counter = count($arr);
for($j=0;$j<$counter; $j++)
{
$arr2 = array("list-1", "list-2", "list-3","list-4", "list-5");
$count = count($arr2);
for($i=0;$count>$i;$i++)
{
//scrapping starts here
$target_url = "http://www.xxxxxxxxx.com.au/buy/".$arr[j]."/".$arr2[i]."?includeSurrounding=false";
$html = new simple_html_dom();
$html->load_file($target_url);
foreach($html->find('a[class=name]') as $vcard)
{
echo $vcard. "<br/>"
}
}
}发布于 2017-07-14 14:07:04
很明显,你不能用C语言编程。PHP的变量以$符号开头。如果没有美元符号,则将它们视为常量。
$arr[j]应为$arr[$j],$arr2[i]应为$arr2[$i]。
发布于 2017-07-14 14:07:13
使用这个-你正在使用一个不带$符号的变量,这就是为什么它被认为是字符串'j‘
<?php
$arr = array("Illawong 2232",
"Strathfield 2135",
"Croydon 2132",
"Croydon Park 2133",
"Burwood 2134",
"Parramatta 2150",
"Hurtsville 2220",
"Seven Hills 2153",
"Blacktown 2148",
"Toongabie 2146",
"Winston Hills 2153",
"Bondi Beach 2026",
"Bondi Junction 2022",
"Coogee 2034",
"Pymble 2073",
"Miranda 2228",
"Caringbah 2229",
"Sylvania 2224",
"Drummoyne 2047",
"Concord 2137"
);
$counter = count($arr);
for($j=0;$j<$counter; $j++)
{
$arr2 = array("list-1", "list-2", "list-3","list-4", "list-5");
$count = count($arr2);
for($i=0;$count>$i;$i++)
{
//scrapping starts here
$target_url = "http://www.xxxxxxxxx.com.au/buy/".$arr[$j]."/".$arr2[$i]."?includeSurrounding=false";
$html = new simple_html_dom();
$html->load_file($target_url);
foreach($html->find('a[class=name]') as $vcard)
{
echo $vcard. "<br/>"
}
}
}发布于 2017-07-14 14:07:54
当你使用variables时,你需要在它们前面使用$符号,否则你会得到一个未定义常量的错误,所以在这里更改
$target_url = "http://www.xxxxxxxxx.com.au/buy/".$arr[$j]."/".$arr2[$i]."?includeSurrounding=false";
//^here //^herehttps://stackoverflow.com/questions/45095772
复制相似问题