【问题标题】:Guzzle HTTP - Skip if Exception Present or Page Not FoundGuzzle HTTP - 如果存在异常或找不到页面,则跳过
【发布时间】:2017-10-06 05:02:57
【问题描述】:

我正在尝试创建一个用于从另一个网站下载 HTML 的 try/catch 循环:

foreach($intldes as $id) {
    $html = HtmlDomParser::file_get_html('https://nssdc.gsfc.nasa.gov/nmc/spacecraftDisplay.do?id='.$id); 
    foreach($html->find('#rightcontent') as $id);
    foreach($html->find('.urone p') as $element);
    foreach($html->find('.urtwo') as $launchdata); 
}

如果数据存在,则生成以下 HTML:

<p><strong>NSSDCA/COSPAR ID:</strong> 2009-038F</p>
<p>ANDE 2, the Atmospheric Neutral Density Experiment 2, is a pair of microsatellites (Castor and Pollux) launched from Cape Canaveral on STS 127 on 15 July 2009 at 22:03 UT and deployed from the payload bay of the shuttle on 30 July 2009 at 17:22 UT.</p>
<p><strong>Launch Date:</strong> 2009-07-15<br/><strong>Launch Vehicle:</strong> Shuttle<br/><strong>Launch Site:</strong> Cape Canaveral, United States<br/></p>

如果数据不存在,我会收到Undefined variable: element 错误,这意味着 DOM Parser 找不到我想要显示的 HTML。

所以我需要一个跳过没有所需 HTML 或返回 NULL 变量的网页的东西。

基本上,如果我想要的 HTML 或变量 $element 不存在,我希望 Guzzle 跳过该网页而不加载它。

编辑:

我的全部功能:

    public function tester() {
    $intldes = DB::table('examples')->pluck('id');
    foreach ($intldes as $query) {
        $html = HtmlDomParser::file_get_html('https://example.com?id='.$query); 
        $elements = $html->find('.urone p', 0);
    if (is_array($elements)) {
        foreach($html->find('#rightcontent') as $rawid);
        foreach($html->find('.urone p') as $rawdescription);
        foreach($html->find('.urtwo') as $launchdata); 

        //-- Data Parser --//
        //Intldes
        $intldesgetter = strip_tags($rawid->first_child()->next_sibling()->next_sibling()); //Get Element and Remove Tags
        $intldesformat = substr($intldesgetter, ($pos = strpos($intldesgetter, ':')) !== false ? $pos + 3 : 0); //Remove Title
        $dbintldes = ltrim($intldesformat); //Remove Blank-space

        //Description
        $description = strip_tags($rawdescription);
        $dbdescription = ltrim($description);

        //Launch Data
        $launchdate = $launchdata->first_child()->next_sibling()->next_sibling()->next_sibling();
        $explode = explode("<br/>", $launchdate);
        $newArray = array_map(function($v){
            return trim(strip_tags($v));
        }, $explode);
        $dblaunchdate = substr($newArray[0], ($pos = strpos($newArray[0], ':')) !== false ? $pos + 3 : 0);
        $dblaunchvehicle = substr($newArray[1], ($pos = strpos($newArray[1], ':')) !== false ? $pos + 3 : 0);
        $dblaunchsite = substr($newArray[2], ($pos = strpos($newArray[2], ':')) !== false ? $pos + 3 : 0);

        //Data Saver
        DB::table('descriptions')->insert(
            ['intldes' => $dbintldes, 'description' => strip_tags($dbdescription), 'launch_date' => $dblaunchdate, 'launch_vehicle' => $dblaunchvehicle, 'launch_site' => $dblaunchsite]
        );
        echo "Success"; 
        } else {
            echo "$query does not exist";
            continue;
        };
    } 
}

【问题讨论】:

    标签: php html laravel dom guzzle


    【解决方案1】:

    我认为您的代码中出现错误:

    foreach($html->find('.urone p') as $element);
    

    根据我的经验,我建议您在迭代 foreach 循环之前首先检查 HTML 标记 的可用性。

    您可以使用is_object()is_array() 来解决您的问题。当您搜索单个元素时,将返回一个对象。当您搜索一组元素时,将返回一个对象数组。

    当您正在搜索一组元素时,您可以使用

    $elements = $html->find('.urone p');
    if (is_array($elements)) {
        //continue
    }
    

    【讨论】:

    • 刚刚试过。如果$elements 确实存在,它就可以工作,但是当它不存在时,它会导致Undefined variable 错误。
    • 你可以试试$element = $html-&gt;find('.urone p', 0),然后检查$element是否为空
    • 不,仍然没有工作 - 我在想我做错了什么。只是为了给你一些关于我遇到的问题的背景知识:我从我的数据库中提取了一个列。然后,我使用列中的数据创建一个 URL foreach - 所以 URL 是 /1、/2、/3 等(我提取的列中的任何内容)。我们目前正在尝试做的是跳过例如 /6 不存在的 URL。我们使用您在问题中提供的 HTML if 语句来执行此操作。
    • 我已经给了你我的全部功能 - 也许你可以看到它有什么问题?谢谢!
    • 您可以动态创建您的 URL,这不是问题。这里明确提到simplehtmldom.sourceforge.net/manual.htm$ret = $html-&gt;find('a', 0);如果找不到就会抛出null
    猜你喜欢
    • 2018-12-29
    • 2019-05-14
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2016-04-10
    相关资源
    最近更新 更多