Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用正则表达式过滤封装在HTML DOM中的JSON值_Php_Jquery_Json_Regex - Fatal编程技术网

Php 使用正则表达式过滤封装在HTML DOM中的JSON值

Php 使用正则表达式过滤封装在HTML DOM中的JSON值,php,jquery,json,regex,Php,Jquery,Json,Regex,我正在编写一个爬虫程序来删除网上商店的价格信息,我使用的是Goutte PHP,它不支持javascript交互,它只抓取静态HTML DOM,因此问题在于我对爬虫程序价格的响应,股票信息隐藏在脚本标记内的HTML DOM中,脚本标记用JSON进一步封装,这是我得到的数据 <script> HZ.productVariation.Manager.setSpaceId('33503761'); HZ.data.Variations.put('33503761', {"availVar"

我正在编写一个爬虫程序来删除网上商店的价格信息,我使用的是Goutte PHP,它不支持javascript交互,它只抓取静态HTML DOM,因此问题在于我对爬虫程序价格的响应,股票信息隐藏在脚本标记内的HTML DOM中,脚本标记用JSON进一步封装,这是我得到的数据

<script>
HZ.productVariation.Manager.setSpaceId('33503761');
HZ.data.Variations.put('33503761', {"availVar": [{"id": "c", "label": "Color", "options": [{"name": "Chrome", "avail": 1, "stock": 1, "price": "$174.51", "quantity": "52", "imageId": "3eb1230d05775d3c"}, {"name": "SuperSteel", "avail": 1, "stock": 0, "price": "$341.40", "quantity": "0", "imageId": "d0a126f505775d3e"}]}], "curVar": {"c": "SuperSteel"}, "exactMatch": true});
HZ.productVariation.Manager.setSelector(HZ.productVariation.ListSelector);
HZ.productVariation.Manager.setRenderer(HZ.productVariation.ViewSpaceRenderer);
HZ.productVariation.Manager.setHistoryManager(new HZ.productVariation.BrowserHistoryManager("replace"));
$('.variationSelectors').append(HZ.productVariation.Manager.drawSelectors('33503761'));

HZ.productVariation.Manager.initUI();
</script>
目前,我正在遍历这个解码的json数组并获取库存状态,我想做的是只编写一个函数来接收密钥,应用正则表达式并返回结果值


是否仍有创建此类型正则表达式的方法?请给出建议,谢谢

解析Json并使用Json解码。
我假设33503761是某种不应该出现在Json中的id

$pos1 =strpos($html, ".put(")+17; // find "put(" and some id? And skip it
$json = substr($html, $pos1, strpos($html, ");", $pos1)-$pos1); //parse json
$arr =json_decode($json,true);
Var_dump($arr);

要查找股票,可以使用array\u walk\u recursive

function test_print($item, $key, $find)
{
    if($key == $find)
        echo "$key holds $item\n";
}

array_walk_recursive($arr, 'test_print', "stock");
代码:()


为什么不从代码中解析Json并将其解码为数组?@Andreas实际上我得到的是完整的HTML页面作为响应,Json在脚本标记中,所以首先我需要从HTML DOM中过滤Json,然后在代码中解析它,每次我得到不同的Json,你得到了吗?是的。。这有什么区别?我的答案对你有帮助吗?是的,你的解决方案有效,但我们能让它更具活力吗?我知道你在职位上增加了17个,但我不明白你为什么要增加17个?这似乎是一个静态解决方案,需要更改输入以过滤另一个JSON结构。我添加了17,因为它将在
.put('33503761',
中找到点的位置,之后有17个字符我不需要。(或者至少它们不是JSON的一部分).我不明白这是怎么一个静态的?也许你的问题缺少细节,我无法给你一个动态的解决方案。例如,33503761是什么?它总是一个8位的代码吗?它能多一些吗?它能少一些吗?这是唯一可能使它静态的变量。你还没有给出任何关于它的信息,所以我不得不假设它总是8位数。对吗?@faraz跟我在一起。这需要解决什么问题?你的html字符串变化有多大?我只能根据你提供的一个示例进行测试。我不想在确认这对你有效之前写一大堆解释。实际上html字符串每次都不同,我的html字符串不同不同的网站,但JSON中有数据,所以我们可以只提供键而不提供任何html字符串信息来获取数据吗?我需要查看一个或两个以上的文本示例。值可以更改,但我需要在结构中找到共性。这可能是html中的另一种JSON格式,比如我给键“values”,它将返回PHP中的values数组。感谢您的帮助。在尝试提取数据之前,您是否知道您将搜索哪些索引和键?我的意思是,对于第一个发布的json,您是否知道您所需的数据位于
$array['availVar'][0]['options']
?对于新提供的json,您是否知道要访问
$array['options'][0]
?很抱歉,我在这里没有得到足够的关注,无法自信地为您创建一个方法。
function test_print($item, $key, $find)
{
    if($key == $find)
        echo "$key holds $item\n";
}

array_walk_recursive($arr, 'test_print', "stock");
function extractColumnData($html,$key){
    if(!preg_match("~\.put\('\d+',\s+(.+?)\);~",$html,$out)){
        return "alert yourself of the preg_match failure";
    }
    if(($array=@json_decode($out[1],true))===null && json_last_error()!==JSON_ERROR_NONE){
       return "alert yourself of the json decode failure";
    }
    return array_column(current(array_column(current($array),'options')),$key,'name');  // this assumes the static structure of your json/array data
}

$html=<<<HTML
<script>
HZ.productVariation.Manager.setSpaceId('33503761');
HZ.data.Variations.put('33503761', {"availVar": [{"id": "c", "label": "Color", "options": [{"name": "Chrome", "avail": 1, "stock": 1, "price": "$174.51", "quantity": "52", "imageId": "3eb1230d05775d3c"}, {"name": "SuperSteel", "avail": 1, "stock": 0, "price": "$341.40", "quantity": "0", "imageId": "d0a126f505775d3e"}]}], "curVar": {"c": "SuperSteel"}, "exactMatch": true});
HZ.productVariation.Manager.setSelector(HZ.productVariation.ListSelector);
HZ.productVariation.Manager.setRenderer(HZ.productVariation.ViewSpaceRenderer);
HZ.productVariation.Manager.setHistoryManager(new HZ.productVariation.BrowserHistoryManager("replace"));
$('.variationSelectors').append(HZ.productVariation.Manager.drawSelectors('33503761'));

HZ.productVariation.Manager.initUI();
</script>
HTML;

echo "avail => ";
var_export(extractColumnData($html,'avail'));
echo "\n----\n";
echo "stock => ";
var_export(extractColumnData($html,'stock'));
echo "\n----\n";
echo "price => ";
var_export(extractColumnData($html,'price'));
echo "\n----\n";
echo "quantity => ";
var_export(extractColumnData($html,'quantity'));
echo "\n----\n";
echo "imageId => ";
var_export(extractColumnData($html,'imageId'));
avail => array (
  'Chrome' => 1,
  'SuperSteel' => 1,
)
----
stock => array (
  'Chrome' => 1,
  'SuperSteel' => 0,
)
----
price => array (
  'Chrome' => '$174.51',
  'SuperSteel' => '$341.40',
)
----
quantity => array (
  'Chrome' => '52',
  'SuperSteel' => '0',
)
----
imageId => array (
  'Chrome' => '3eb1230d05775d3c',
  'SuperSteel' => 'd0a126f505775d3e',
)