当输出是无序的html标记时,使用简单html dom的Php webscraping不起作用
我想删除网页的一些信息。它使用表格布局结构 我想提取嵌套表布局中的第三个表,其中包含一系列嵌套表。每个表都发布一个结果。但代码不起作用当输出是无序的html标记时,使用简单html dom的Php webscraping不起作用,php,web-scraping,simple-html-dom,Php,Web Scraping,Simple Html Dom,我想删除网页的一些信息。它使用表格布局结构 我想提取嵌套表布局中的第三个表,其中包含一系列嵌套表。每个表都发布一个结果。但代码不起作用 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 $resu
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);
函数curl($url){
$ch=curl_init();//初始化curl
curl_setopt($ch,CURLOPT_URL,$URL);//使用传入函数的$URL变量设置curl的URL选项
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);//设置curl的选项以返回网页数据
$data=curl\u exec($ch);//执行curl请求并将返回的数据分配给$data变量
curl_close($ch);//Closing curl
return$data;//从函数返回数据
}
函数在($data、$start、$end)之间进行刮取{
$data=stristr($data,$start);//从$start之前剥离所有数据
$data=substr($data,strlen($start));//剥离$start
$stop=stripos($data,$end);//获取要刮取的数据$end的位置
$data=substr($data,0,$stop);//从后面剥离所有数据,包括要刮取的数据的$end
return$data;//从函数返回刮取的数据
}
$scraped_page=curl($url);//执行curl函数来刮取网页http://www.example.com 并将结果返回到$scraped_网站变量中
$scraped_data=scrape_在($scraped_page,,)之间
');
echo$scraped_数据;
$myfile=fopen(“newfile.html”,“w”)或die(“无法打开文件!”);
fwrite($myfile,$scraped_数据);
fclose($myfile);
如何刮取结果并保存pdf
查找示例代码
?php
//定义基本旋度函数
函数curl($url){
$ch=curl_init();//初始化curl
curl_setopt($ch,CURLOPT_URL,$URL);//使用传入函数的$URL变量设置curl的URL选项
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);//设置curl的选项以返回网页数据
$data=curl\u exec($ch);//执行curl请求并将返回的数据分配给$data变量
curl_close($ch);//Closing curl
return$data;//从函数返回数据
}
?>
简单HTML Dom无法处理该HTML。那么首先切换到,,
然后做:
我之前尝试过curl,但我没有在这里提到它,这是我的错curl成功地返回了我的抓取页面,但它无法使用简单的hitml dom元素进行解析,因为它不知怎么地变形了。有多个主体标记,里面有脚本和其他乱七八糟的内容。简言之,即使它返回混乱的html标记和关于反对票,在你编辑之前我不能取消它。你测试过了吗?还有一点解释是的,我测试过了。这都是css(加上jquery扩展)
td[2]
例如,意味着第二个td
我已将其升级,你能帮我用正则表达式提取发布日期吗?接受它作为答案别忘了我的赏金
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);