0
シンプルなHTML Domを使用してPHPでスクレーパーを作成しました。シンプルなHTML DOM、スクレイピングエラー注意:C: xampp htdocs scraper au_div_puller.phpのオブジェクトでないオブジェクトのプロパティを取得しようとしています
問題は、それが結果を返しますが、私にエラーを与えることである
、誰もがそれを修正する方法についての正しい方向に私を指す エラーがあるしてください。
お知らせ:しようとしていますCで非オブジェクトのプロパティを取得:\ xamppの\ htdocsに\スクレーパー\ au_div_puller.phpライン60
多くトンにハンクス ライン60がある
$ Ex_Date = $ TR->見つける( 'TD' が、0) - >平文; //最初の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 == " ") 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();
正しい結果であるかどうか$ htmlを確認してください – Pratansyah
は '
@Forbsが言ったことを拡張するために、エラーはオブジェクトとして書いたものが実際にオブジェクトではないということです。例えば、 '$ a-> plaintext'のようなものを書くと、$ aは' plaintext'という名前のプロパティを持つオブジェクトになると考えています。しかし '$ a'が本当にnullの場合はどうでしょうか? '非オブジェクトのプロパティ'エラーが発生します。したがって、あなたの場合、 '$ tr'はオブジェクトですが、' find( 'td'、0) 'メソッドは何も見つからず、オブジェクトを返しませんでした。したがって、 'find( 'td'、0) - > plaintext'を実行しようとすると、ヌル値のプロパティを見つけようとしているので文句を言います。 –
答えて
をオフにコピーされたことを使用することができ、準備されたステートメントである
find('td', 0)
価値がある場合は、tdが見つからない場合は、plaintext
のプロパティを要求しないように保証する必要があります。確かに、それはそれらを一緒に連鎖するほどセクシーではありませんが、あなたは最初の方法(私の例では
$td
)は常にメソッド/プロパティあなたを持つオブジェクトを返すことを知って場合にのみ、連鎖して動作します再呼び出し。さらに、変数
$sql
に値を書き込むのではなく、プリペアドステートメント(values(?,?,?,?,?,?,?,?)
)を使用します。出典
2017-11-17 04:36:02
あなたのコードには非常に感謝しています。それを見ていただきありがとうございます。私はコードを更新しましたが、エラーはありません:))))))私は以下のようなことをすることができますが、どこに配置するのか分かりますか? – Thomas
'code'を参照してください//準備とバインド $ stmt = $ conn-> prepare(" INSERT INTO MyGuests(firstname、lastname、email) )VALUES(?、?、?) "); $ stmt-> bind_param( "sss"、$ firstname、$ lastname、$ email); //パラメータを設定して実行 $ firstname = "John"; $ lastname = "Doe"; $ email = "john @ example。comの "; $ stmt->()を実行し、 の$ FIRSTNAME = "メアリー"; $ の姓= "萌え"; $ メール= "[email protected]"; $ stmt->実行(); の$ FIRSTNAME = "ジュリー"; $の姓= "ドゥーリー"; $ Eメール= "[email protected]"; $ stmt->()を実行し、 は、 "正常に作成新しいレコードを" エコー; $ stmt-> close(); $ conn-> close(); 'code' – Thomas
欠けている部分がパラメータをバインドしています。TBH、ネイティブのPHPコマンドは使用しません。私はあまりに精通していないので、どのようにさまざまなmysqli、 pdoなどのコマンドが動作します。しかし、PDOを使用している場合、パラメータはexecuteメソッドによって配列に送られます。例2を参照してください。http://php.net/manual/en/pdo.prepare.php –
関連する問題