阅读网址列表并阅读div

时间:2017-11-15 16:51:40

标签: php mysql

我试图在网址列表中读取一些div的数据,但我没有找到正确的方法,实际上我只得到一个白页,但如果在注释代码上执行print_r($ aElements)我可以看到网址加载正确,所以我认为错误是在foreach上,但我无法修复它,我需要div内的链接id = city     

require_once('simple_html_dom.php');
set_time_limit (2000);

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "buss";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT url FROM business";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    $html = file_get_html($row["url"]);
    $aElements = $html->find('div[id=city]', 0)->find('a');
    $localList = array();
// here if i do a print_r i get the html of each url listed and is fine (of $aElements)
    foreach($aElements as $element) {
        if(substr($element->href, 0, 1) == '/') {
            $ownerName = trim(str_replace('Punto de', '', $element->plaintext));
            $localList[$artistName] =   $element->href;


            $file = fopen("newfile.txt", "a");
            fwrite($file, $localList[$ownerName] =  $element->href . PHP_EOL);
            fclose($file);
        }

     }
    }
} else {
    echo "0 results";
}
$conn->close();

0 个答案:

没有答案