【问题标题】:Parsing, using curl and xpath in PHP, an HTML page with before a form解析,在 PHP 中使用 curl 和 xpath,在表单之前使用 HTML 页面
【发布时间】:2017-07-17 19:01:39
【问题描述】:

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

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

url页面如下

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

这里是上述选择和操作后的页面图像

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

【问题讨论】:

    标签: php parsing curl xpath html-parsing


    【解决方案1】:

    在 PHP 中,您可以使用 curl 将表单的数据发布到与表单操作相同的 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 脚本示例,inspiré de https://davidwalsh.name/curl-post(您必须安装 curl 才能运行此示例):

    <?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);
    

    【讨论】:

    • 你有一个关于如何做的小样本吗?我是 PHP 的新手……谢谢! (无论如何,您对使用 PhantomJS 的第一个建议可能很有趣......)
    • 如果你使用的是 JS,phantomJS 可以很好地实现页面自动化,但你的问题是关于 PHP 的 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    相关资源
    最近更新 更多