php 循环抓取网页内容(PHP抓取页面上的数组并循环输出急在线等我用file_get_contents)

优采云 发布时间: 2022-01-13 04:18

  php 循环抓取网页内容(PHP抓取页面上的数组并循环输出急在线等我用file_get_contents)

  PHP抓取页面上的数组,紧急循环输出在线等

  我使用 file_get_contents() 来爬取这个 URL 的内容

  它似乎正在返回一个数组。 但是无论我如何使用 foreach 循环,我都会遇到错误。 .

  我只想获取数组中单词中的值。 谁能帮帮我,急

  ------解决方案----------

  $s = file_get_contents('#39;);

  preg_match_all('/\[word\] => (.+)/', $s, $m);

  print_r($m[1]);数组

  (

  [0] => 1314

  [1] => abc

  )

  ------解决方案----------

  $s=file_get_contents('#39;);

  $rule='#(?)\s\w+#';

  preg_match_all($rule,$s,$arr);

  print_r($arr);

  数组

  (

  [0] => 数组

  (

  [0] => 1314

  [1] => abc

  )

  )

  ------解决方案----------

  返回的是:

  string(247) "数组 ( [0] => 数组 ( [字] => 1314 [字标签] => 90 [索引] => 0 ) [1] => 数组 ( [字] => abc [word_tag] => 95 [index] => 1 ) ) "

  //数组结构的字符串,不是数组

  //编码

  $arr = 数组(

  0=>数组(

  '单词'=> 1314,

  'word_tag'=> 90,

  '索引' => 0

  ),

  1 => 数组(

  '单词' => 'abc',

  'word_tag' => 95,

  '索引' => 1

  )

  );

  echo(json_encode($arr));

  //解码

  $arr = 数组();

  $url = '#39;;

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_HEADER, 0);

  $output = curl_exec($ch);

  $arr = json_decode($output,true);

  curl_close($ch);

  您也可以使用 serialize() 和 unserialize() 序列化函数来代替 json。

  ------解决方案----------

  补充:

  //json返回的字符串

  [{"word":1314,"word_tag":90,"index":0},{"word":"abc","word_tag":95,"index":1}]

  //序列化返回的字符串

  a:2:{i:0;a:3:{s:5:"word";i:1314;s:8:"word_tag";i:90;s:5:"index";i :0;}i:1;a:3:{s:4:"word";s:3:"abc";s:8:"word_tag";i:95;s:5:"index";i :1;}}

  明显短于直接 var_export($val,true);输出,并且可以很容易地恢复。

  相关文章

  相关视频

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线