2017-02-27 54 views
0

我使用PHP和xpath来解析一些HTML页面:在上一期(rif。Parsing an HTML page using curl and xpath in PHP)中,我已经解决了如何解析页面来提取一些值。解析,在PHP中使用curl和xpath,在表单之前使用HTML页面

现在我已经在另一个页面中,在获取我想要解析的值之前,我必须选择一个值(图片中的Venezia ...,组合框“Provincia”中的... ...) ),然后点击一个按钮(图片中的“CERCA”...),然后获得我想要解析的值(即页面中红色,绿色和黄色框中的数字...)。 。)

的URL页面是后续

https://salute.regione.veneto.it/servizi/situazione-nei-pronto-soccorso

,在这里你上面

0123所描述的选择和行动之后是页面的图像

enter image description here

是否有可能,以及如何在PHP中,模拟这个HTML导航序列,以获得比我来解析HTML页面?

回答

1

在PHP中,你可以使用卷曲在相同的URL,表单动作后表单的数据:https://salute.regione.veneto.it/servizi/situazione-nei-pronto-soccorso?p_p_id=PRONTOSOCCORSO_WAR_portalprontosoccorso_INSTANCE_o0QZ&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-3&p_p_col_count=1

,并取回HTML页面。

php脚本,激发德https://davidwalsh.name/curl-post的实例(你必须安装卷曲得到工作本示例):

<?php 

$url = 'https://salute.regione.veneto.it/servizi/situazione-nei-pronto-soccorso?p_p_id=PRONTOSOCCORSO_WAR_portalprontosoccorso_INSTANCE_o0QZ&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-3&p_p_col_count=1'; 

$fields = array(
    'ulss'   => '101', 
    'provincia'  => 'BL', 
    'nomPS'   => '', 
    'rossoInAttesa' => '', 
    'gialloInAttesa' => '', 
    'verdeInAttesa' => '', 
    'biancoInAttesa' => '' 
); 

//url-ify the data for the POST 
$fields_string = ""; 
foreach($fields as $key=>$value) { 
    $fields_string .= $key.'='.$value.'&'; 
} 

rtrim($fields_string, '&'); 

//open connection 
$ch = curl_init(); 

//set the url, number of POST vars, POST data 
curl_setopt($ch,CURLOPT_URL, $url); 
curl_setopt($ch,CURLOPT_POST, count($fields)); 
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 


//execute post 
$result = curl_exec($ch); 

file_put_contents('result_page.html', $result); 

//close connection 
curl_close($ch); 
+0

你已经就如何做到这一点的样品?我在PHP中是一个新手...谢谢! (无论如何,你的第一个使用PhantomJS的建议可能会很有趣......) – Cesare

+1

如果你使用JS,phantomJS可以很好的做页面自动化,但你的问题是关于PHP的,所以;) – nicolastorre

+0

可能是一个有趣的替代方案... 。感谢代码示例 – Cesare