我是抓取的初学者,我正在使用PHP simple_html_dom从网站上抓取数据。我当前的代码没有显示任何结果。也许我没有针对正确的html标签。第二件事是我需要如果搜索查询没有结果,则代码显示消息:"未找到结果"或类似的东西。任何帮助表示赞赏。
以下是示例查询:
3lnhl2gc9br764854 1J4FF28SXXL550156
<?php
require "simple_html_dom.php";
$trazi=$_POST['trazi'];
$url="http://lookupvin.com/check/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"VIN=$trazi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close($ch);
$html = str_get_html($server_output);
foreach($html->find('p.nmar') as $element)
echo $element->innerText();
?>
index.php
<form action="vin.php" method="POST">
<input type="text" name="trazi">
<input type="submit">
</form>
答案 0 :(得分:1)
include "simple_html_dom.php";
$trazi="1J4FF28SXXL550157";
$url="http://lookupvin.com/check/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "VIN=$trazi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close($ch);
$html = new simple_html_dom();
$html->load($server_output);
$items = $html->find('.nmar');
if(count($items)!=0) {
foreach($items as $post) {
echo $post->children(0);
echo "<br>";
}
}
else {
echo "Wrong Input";
}
在线找到了更好的html解析。 http://code.tutsplus.com/tutorials/html-parsing-and-screen-scraping-with-the-simple-html-dom-library--net-11856您可以从此处下载。
答案 1 :(得分:0)
尝试删除CURLOPT_POST和CURLOPT_POSTFIELDS参数。服务器响应GET请求,无需POST。您作为查询字符串的一部分正确传递参数。 另请查看http://php.net/manual/en/function.curl-error.php,这可以帮助您检查您的请求是否成功。