【问题标题】:i can't access to an span with parent class name我无法访问具有父类名的跨度
【发布时间】:2018-04-08 20:12:04
【问题描述】:

我试试这段代码,但它返回 NULL 对象!

<?php
ini_set('max_execution_time', 300);
include_once ('Scraper.php');
$scraper = new Scraper();
$pageUrl = 'https://www.zara.com/tr/en/wrap-blouse-with-bow-p04437059.html?v1=6022035&v2=943001';
$pageHtmlContent = $scraper->curl($pageUrl);
$dom=new DOMDocument();
$dom->load($pageHtmlContent);
$xpath=new DOMXPath($dom);
$prise= $xpath->evaluate('(//div[@class=\'._product-price\'])');
var_dump($prise);
?>

我使用 selectorGadget 来制作 XPATH 地址。

【问题讨论】:

    标签: php xpath web-scraping


    【解决方案1】:

    抱歉,无法添加评论,没有足够的声誉:(。

    您的 xpath 不正确,我在给定的 url 中找不到任何内容。请提供您希望找到的内容。

    编辑以发表评论

    要找到准确的 div(正如我所尝试的,在给定的 url 中只有 1 个具有特定类的 div),请使用此 xpath (//*[contains(@class,'_product-price')])。如果您需要该 div 中的价格,请使用此 xpath (//*[contains(@class,'_product-price')]/span)。如果有超过 1 个 xpath,请使用此 (//*[contains(@class,'_product-price')]/span)[i],其中 i 在 DOM 元素的循环内。

    例如

    dom_elements = [get_dom_elements]
    store_value = []
    i = 1
    while i < len(dom_elements):
        xpath = '(//*[contains(@class,'_product-price')]/span)[i]'
        store_value.append(some_function_for_getting_value_of_dom(xpath))
        i += 1
    

    【讨论】:

    • 我需要通过“._product-price”类名访问一个div的内容。
    • 附注我的答案是在 Python 中,但是你可以在 PHP 中实现同样的方式:)
    猜你喜欢
    • 2010-11-21
    • 2016-02-11
    • 2017-12-01
    • 2023-04-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 2017-07-15
    • 2015-07-28
    相关资源
    最近更新 更多