自动点击按钮和网页抓取


Auto click button and web scraping

我试图从网页中获取一些数据,并将其放入数组(php或javascript)或数据库中。

该页面的链接为:https://pilotweb.nas.faa.gov/PilotWeb/

我的问题是,我希望系统本身按下"我同意"按钮,然后在位置字段中填写"LGGG"一词。然后推送"查看注释"以获得结果。

从结果中,我需要检索粗体和一些坐标的名称。

我尝试了"使用jQuery加载页面时自动点击按钮元素"链接中的说明,但没有成功。

任何建议都会有帮助!

我找到了问题的答案,并将其发布,以防其他人面临同样的问题。

您所需要的只是页面中的客户端PHP URL:http://php.net/manual/en/book.curl.php

我的案例代码是:

define("CURL_AGENT", "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
$c = curl_init();
curl_setopt_array($c, array(
    CURLOPT_HEADER => 0,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_USERAGENT => CURL_AGENT,
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_SSL_VERIFYPEER => 0,
    CURLOPT_COOKIEFILE => 'NULL',
    CURLOPT_COOKIEJAR  => 'NULL',
    CURLOPT_FOLLOWLOCATION => 1,
    //the link i want to retrieve data
    CURLOPT_URL => 'https://pilotweb.nas.faa.gov/PilotWeb/',
    CURLOPT_COOKIESESSION => 1
));
$resp = curl_exec($c);
//from source code i get what I need
$post = 'formatType=DOMESTIC&retrieveLocId=LGGG&reportType=REPORT&openItems=icaosHeader%2Cicaos%3AicaoHead%2Cicao%3ArightNavSec0%2CrightNavSecBorder0%3A&actionType=notamRetrievalByICAOs&submit=View+NOTAMs';
curl_setopt_array($c, array(
    CURLOPT_POST => 1,
    //the link of target page
    CURLOPT_URL => 'https://pilotweb.nas.faa.gov/PilotWeb/notamRetrievalByICAOAction.do?method=displayByICAOs',
    CURLOPT_POSTFIELDS => $post
));
$resp = curl_exec($c);
curl_close($c);

上面的代码创建了一个HTML文件,其中包含页面的结果。

如果您可以使用jQuery并在页面中注入代码,并且代码可以是jQuery,那么您需要的代码行如下:

   $(function () {
        // on ready check the href
        if (window.location.href == 'https://pilotweb.nas.faa.gov/PilotWeb/') {
            // if cookie does not exist --> push I Agree 
            if (document.cookie.match(/^.*PILOTWEB_DISCLAIMER=true$/) === null) {
                $("button:contains('I Agree')").trigger('click');
            }
            // set text
            $('textarea[name="retrieveLocId"]').text('LGGG');
            // submit form
            $('form[action="/PilotWeb/notamRetrievalByICAOAction.do?method=displayByICAOs"] input[value="View NOTAMs"]').trigger('click');
        } else {
            // on second page get the results
            if (window.location.href == 'https://pilotweb.nas.faa.gov/PilotWeb/notamRetrievalByICAOAction.do?method=displayByICAOs') {
                $('[id="notamRight"] span strong').each(function(index, element) {
                    var boldValue = $(this).text();
                    // save it
                });
            }
        }
    });