简单的 html dom,抓取错误注意:尝试在第 60 行的 C:\xampp\htdocs\scraper\au_div_puller.php 中获取非对象的属性

托马斯

我使用简单的 html Dom 在 php 中编写了一个刮板。

问题是它返回结果但给了我一个错误

任何人都可以指出我如何解决它的正确方向错误是:

注意:尝试在第 60 行的 C:\xampp\htdocs\scraper\au_div_puller.php 中获取非对象的属性

非常感谢第 60 行是

$Ex_Date = $tr->find('td', 0)->plaintext; // 找到第一个TD(从0开始)

 <?php
    //REQUIRED FILES
    require ('connect_mysql.php');
    require('simple_html_dom.php');

//SET VARIABLES OF WEBSITE TO CRAWL
$url = ('http://www.shares.com/ANZ');  //WEBSITE TO SCRAPE WITH MYSQL INJECTED FROM ABOVE
echo ($url . "<br>");

//SET USER AGENT TO BE GOOGLEBOT
        $opts = array  ('http'=>array(  'method'=>"GET", 'header' => 'User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', ));
        $context = stream_context_create($opts);
        //$html = new simple_html_dom();
        $response = file_get_html($url, false, $context);
        $html = str_get_html($response);

//CHECK IT IS NOT A 404 PAGE IF SO SKIP
if (!empty($html)) {
//CHECK IT IS NOT BLANK PAGE OR EMPTY PAGE IF SO SKIP
$count = count($html->find('table'));
if($count > 0){

//START TABLE PROCESSING
$table = $html->find('table', 0); // ID LOCK IE TABLE 0 (first table) Get the first table ??
foreach($table ->find('tr') as $tr) {     // Foreach row in the table!
$Ex_Date = $tr->find('td', 0)->plaintext; // Find the first TD (starts with 0)
if($Ex_Date == "" || $Ex_Date == "&nbsp;") continue;  // Don't allow empty records
$Amount = $tr->find('td', 1)->plaintext; // Find the second TD (which will be 1)
$Franked = $tr->find('td', 2)->plaintext; // Find the third TD (which will be 2)
$Franking_Credit = $tr->find('td', 3)->plaintext; // Find the fourth TD (which will be 3)
$Books_Close = $tr->find('td', 4)->plaintext; // Find the fifth TD (which will be 4)
$Date_Payable = $tr->find('td', 5)->plaintext; // Find the sixth TD (which will be 5)


//MYSQL DATA FORMATTING
//ESCAPE STRINGS AND DATE FORMATTING
//Now validate the data with mysqli_real_escape_string(). This function will escape characters that cause problems, like single quotes.
//Note there needs to be an open connection to the MySQL server for this work, otherwise you'll have blank strings returned.
// convert 04-Dec-1997 to yyyy-mm-dd formate
// for other versions of date format see:   https://stackoverflow.com/questions/16139696/convert-date-to-mysql-date-format-php
$Ex_Date_c = mysqli_real_escape_string($conn, $Ex_Date);
    $Ex_Date_c = date('Y-m-d', strtotime($Ex_Date_c)); //fix date format
$Amount_c = mysqli_real_escape_string($conn, $Amount);
$Franked_c = mysqli_real_escape_string($conn, $Franked);
$Franking_Credit_c = mysqli_real_escape_string($conn, $Franking_Credit);
$Books_Close_c = mysqli_real_escape_string($conn, $Books_Close);
    $Books_Close_c = date('Y-m-d', strtotime($Books_Close_c));//fix date format
$Date_Payable_c = mysqli_real_escape_string($conn, $Date_Payable);
    $Date_Payable_c = date('Y-m-d', strtotime($Date_Payable_c));//fix date format


//MYSQL INSERT TIME AND TESTING
//MYSQL INSERT QUERY
$sql = "INSERT INTO $insertintotable    (stockcode, exchange, exdate, amount, franked, frankingcredit, booksclose, datepayable, updatedatetime)
                            VALUES      ('$stockcode', 'ASX', '$Ex_Date_c', '$Amount_c', '$Franked_c', '$Franking_Credit_c', '$Books_Close_c', '$Date_Payable_c', '$updatedatetime')";
//MYSQL RESULT TEST
//echo ($sql . "<br>");  // Show the Mysql query

 if ($conn->query($sql) === TRUE)   {
 //                                 echo "New record created successfully <br>";  //TESTING --- Uncomment this code after verifying that the echo statements produce valid INSERT queries.
                                    }
                                    else {echo "Error: " . $sql . "<br>" . $conn->error;}

}
}
}
}
// CLOSE AND CLEAR SESSION
$html->clear();
unset($html);
}
$conn->close();
?>

这是我可以使用的准备好的声明,我是从学习网站上复制的

// prepare and bind
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);

// set parameters and execute
$firstname = "John";
$lastname = "Doe";
$email = "[email protected]";
$stmt->execute();

$firstname = "Mary";
$lastname = "Moe";
$email = "[email protected]";
$stmt->execute();

$firstname = "Julie";
$lastname = "Dooley";
$email = "[email protected]";
$stmt->execute();

echo "New records created successfully";

$stmt->close();
$conn->close();
蒂姆·莫顿

由于您不能保证find('td', 0)会找到一个值,因此您必须保证plaintext如果没有找到 td ,您不会尝试去请求该属性

$table = $html->find('table', 0); // ID LOCK IE TABLE 0 (first table) Get the first table ??
foreach($table ->find('tr') as $tr) {     // Foreach row in the table!
    if($td = $tr->find('td', 0)) {
        $Ex_Date = $td->plaintext; // Find the first TD (starts with 0)
        // ... and so on for each variable

当然,它不像将它们链接在一起那么性感,但是只有当您知道第一个方法($td在我的示例中)将始终返回具有您正在调用的方法/属性的对象时,链接才有效

附带说明一下,您还应该考虑使用准备好的语句 ( values(?,?,?,?,?,?,?,?)) 而不是将值粘贴到$sql变量中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档