【问题标题】:How to get file_contents from each url of an urls array如何从 urls 数组的每个 url 获取 file_contents
【发布时间】:2015-12-18 08:23:11
【问题描述】:

我试图弄清楚如何返回数组(urls_array)中每个 url 的 file_contents。到目前为止,使用 simplehtmpdom 的以下代码只给了我一个结果,然后代码无法在 foreach 循环中运行。

$urlsall = 'http://php.net,
http://php.net/downloads,
http://php.net/docs.php,
http://php.net/get-involved,
http://php.net/support,
http://php.net/manual/en/getting-started.php,
http://php.net/manual/en/introduction.php,
http://php.net/manual/en/tutorial.php,
http://php.net/manual/en/langref.php,
http://php.net/manual/en/language.basic-syntax.php,
http://php.net/manual/en/language.types.php,
http://php.net/manual/en/language.variables.php,
http://php.net/manual/en/language.constants.php,
http://php.net/manual/en/language.expressions.php,
http://php.net/manual/en/language.operators.php,
http://php.net/manual/en/language.control-structures.php,
http://php.net/manual/en/language.functions.php,
http://php.net/manual/en/language.oop5.php,
http://php.net/manual/en/language.namespaces.php,
http://php.net/manual/en/language.errors.php,
http://php.net/manual/en/language.exceptions.php,
http://php.net/manual/en/language.generators.php,
http://php.net/manual/en/language.references.php,
http://php.net/manual/en/reserved.variables.php,
http://php.net/manual/en/reserved.exceptions.php,
http://php.net/manual/en/reserved.interfaces.php,
http://php.net/manual/en/context.php';

$urls_array = explode(',', $urlsall);
//var_dump ($urls_array);
foreach ($urls_array as $url)
    {

         $html = SimpleHtmlDom::file_get_html($url); 
         $title = $html->find('title',0);
        echo $title->plaintext; 

    }

结果:PHP:超文本预处理器

ERROR: An error occured, The error has been reported.
Error on Dec 18, 2015 17:16PM - file_get_contents( http://php.net/downloads): failed to open stream: Invalid argument in E:\xampp\htdocs\sitename\SimpleHtmlDom.php on line 81

我想做的是从上面的 foreach 循环中获取所有 url 标题

【问题讨论】:

  • 网址标题到底是什么意思?可以举个例子吗?
  • 你为什么要抓取 php 手册?
  • 为什么要创建一个带有 url 的字符串,而不是预先声明数组?在逗号上爆炸不会修剪大多数 URL 现在将以新行开头的字符串。这就解释了为什么您在第二个 url 上遇到错误
  • @Sina ... 这个 url: 'php.net 当你得到 $title = $html->find('title',0) 时,它会给你页面标题: PHP: Hypertext预处理器
  • 他的意思是那些url链接的html页面的标题。

标签: php arrays url file-get-contents simple-html-dom


【解决方案1】:

就像我在评论中所说:从外观上看,问题的最可能原因是您在字符串上使用了explode,使用逗号作为分隔符。但是,您的字符串也包含很多空格,您没有对其进行修剪。这可以解释为什么第一个 URL 没有错误通过,但第二个失败(该 url 以换行符开头)。

我建议你要么定义一个 array 的 url,而不是你爆炸的字符串,或者你修剪所有的 url:

$urls = array_map('trim', explode(',', $urlsall));

这将为explode 返回的数组中的每个值调用trim。不过,这有点傻。你一开始就对 url 进行硬编码,那么为什么不写一个数组而不是一个长字符串呢?

$urls = array(
    'http://php.net',
    'http://php.net/downloads',
    'http://php.net/docs.php',
    'http://php.net/get-involved',
    'http://php.net/support',
    'http://php.net/manual/en/getting-started.php',
    //rest of the urls here
);

【讨论】:

  • 它在修剪后工作。谢谢。虽然它很慢......有什么建议可以更快地做到这一点吗?
  • @OjObasi:您可以查看使用file_get_contents + DOMDocument 是否有效,而不是依赖这个SimpleHtmlDom 类(不知道它来自哪里)。它可以加快速度,但我怀疑开销的主要来源是网络延迟:您实际上是在下载所有这些页面,想想在浏览器中导航到每个页面需要多长时间。一页并不长,但即使是半秒,乘以页数,也很容易达到几秒
【解决方案2】:

您收到此错误是因为您的数组中有一些换行符。 当您对数组进行 var_dump 时,我得到:

array (size=27)
   0 => string 'http://php.net' (length=14)
   1 => string '
      http://php.net/downloads' (length=26)
   2 => string '
       http://php.net/docs.php' (length=25)
   3 => string '
       http://php.net/get-involved' (length=29)

你为什么使用爆炸? 直接制作一个数组来执行此操作:

$urlsall = array(
'http://php.net',
'http://php.net/downloads',
'http://php.net/docs.php',
'http://php.net/get-involved',
'http://php.net/support',
'http://php.net/manual/en/getting-started.php',
'http://php.net/manual/en/introduction.php',
'http://php.net/manual/en/tutorial.php',
'http://php.net/manual/en/langref.php',
'http://php.net/manual/en/language.basic-syntax.php',
'http://php.net/manual/en/language.types.php',
'http://php.net/manual/en/language.variables.php',
'http://php.net/manual/en/language.constants.php',
'http://php.net/manual/en/language.expressions.php',
'http://php.net/manual/en/language.operators.php',
'http://php.net/manual/en/language.control-structures.php',
'http://php.net/manual/en/language.functions.php',
'http://php.net/manual/en/language.oop5.php',
'http://php.net/manual/en/language.namespaces.php',
'http://php.net/manual/en/language.errors.php',
'http://php.net/manual/en/language.exceptions.php',
'http://php.net/manual/en/language.generators.php',
'http://php.net/manual/en/language.references.php',
'http://php.net/manual/en/reserved.variables.php',
'http://php.net/manual/en/reserved.exceptions.php',
'http://php.net/manual/en/reserved.interfaces.php',
'http://php.net/manual/en/context.php'

);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2011-05-26
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2016-04-13
    相关资源
    最近更新 更多