使用简单的html dom的php webscraping在输出无序的html标签时不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用简单的html dom的php webscraping在输出无序的html标签时不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2445字,纯文字阅读大概需要4分钟。
内容图文
![使用简单的html dom的php webscraping在输出无序的html标签时不起作用](/upload/InfoBanner/zyjiaocheng/827/b5c745333bfa48b1ba8cefb483a04e69.jpg)
我想废弃网页的一些信息.它使用表格布局结构.
我想在嵌套表格布局中提取第三个表格,其中包含一系列嵌套表格.每个发布一个结果.但代码不起作用
include('simple_html_dom.php');
$url = 'http://exams.keralauniversity.ac.in/Login/index.php?reslt=1';
$html = file_get_contents($url);
$result =$html->find("table", 2);
echo $result;
我使用Curl来提取网站,但问题是它的标签是乱序的,因此无法使用简单的dom元素提取它.
function curl($url) {
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL,$url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}
function scrape_between($data, $start, $end){
$data = stristr($data, $start); // Stripping all data from before $start
$data = substr($data, strlen($start)); // Stripping $start
$stop = stripos($data, $end); // Getting the position of the $end of the data to scrape
$data = substr($data, 0, $stop); // Stripping all data from after and including the $end of the data to scrape
return $data; // Returning the scraped data from the function
}
$scraped_page = curl($url); // Executing our curl function to scrape the webpage http://www.example.com and return the results into the $scraped_website variable
$scraped_data = scrape_between($scraped_page, ' </html>', '</table></td><td></td></tr>
</table>');
echo $scraped_data;
$myfile = fopen("newfile.html", "w") or die("Unable to open file!");
fwrite($myfile, $scraped_data);
fclose($myfile);
如何刮取结果并保存pdf
解决方法:
简单的HTML Dom无法处理该HTML.所以先切换到this library,
然后做:
require_once('advanced_html_dom.php');
$dom = file_get_html('http://exams.keralauniversity.ac.in/Login/index.php?reslt=1');
$rows = array();
foreach($dom->find('tr.Function_Text_Normal:has(td[3])') as $tr){
$row['num'] = $tr->find('td[2]', 0)->text;
$row['text'] = $tr->find('td[3]', 0)->text;
$row['pdf'] = $tr->find('td[3] a', 0)->href;
if(preg_match_all('/\d+/', $tr->parent->find('u', 0)->text, $m)){
list($row['day'], $row['month'], $row['year']) = $m[0];
}
// uncomment next 2 lines to save the pdf
// $filename = preg_replace('/.*\//', '', $row['pdf']);
// file_put_contents($filename, file_get_contents($row['pdf']));
$rows[] = $row;
}
var_dump($rows);
内容总结
以上是互联网集市为您收集整理的使用简单的html dom的php webscraping在输出无序的html标签时不起作用全部内容,希望文章能够帮你解决使用简单的html dom的php webscraping在输出无序的html标签时不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。