【问题标题】:how to use online CSV file data as value without downloading the file如何在不下载文件的情况下使用在线 CSV 文件数据作为值
【发布时间】:2012-08-23 20:42:01
【问题描述】:

您好,我有一个下载 CSV 文件的链接,我想使用里面的数据来建立我的网站,但我不会下载文件,有什么办法吗? 链接是:http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download

【问题讨论】:

    标签: c# php


    【解决方案1】:

    您需要获取文件才能获取其中的数据。

    之后让您的服务器将其删除。

    【讨论】:

    • 您可以通过WebClient.OpenRead() 打开文件流并将结果分配给流(见我的回答)。然而,这是 C#。
    【解决方案2】:

    您可以在运行时使用 cURL 获取文件的内容。

    $url = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download";
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    

    $data 现在将包含该文件的内容。

    【讨论】:

    • 我只是试了一下,结果是“致命错误:调用未定义的函数 curl_init()”有什么我忘了添加代码:' 无标题文档 nasdaq.com/screening/…"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $data = curl_exec($ch); curl_close($ch);回显$数据; ?> '
    • @PriscillaIp 如果您收到一条错误消息,指出 curl_init() 未定义,则表示您的服务器上未安装 PHP cURL 库。如果您不能/不想安装它,请尝试使用 Stegrex 的 fopen() 解决方案。
    • 你知道安装 cURL 库的好方法吗?
    • @PriscillaIp Priscilla,请阅读thisthis。它们是有关如何启用 cURL(如果已安装)以及如何安装 cURL 的说明。另请查看 PHP 手册中的 cURL installation docs
    【解决方案3】:

    使用 fopen() 或 PHP cURL 库:

    fopen() (通常这不如 cURL 安全,当 PHP 设置文件中不允许使用 fopen 时,您可能会遇到问题,但这是一种快速而肮脏的方法):

    // Load file into resource from URL.
    $fileHandle = fopen("http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download", "r");
    // Edit: Load file resource into string.
    $fileContents = fread($fileHandle);
    echo $fileContents;
    

    cURL(首选方式。在此处阅读 cURL:http://php.net/manual/en/book.curl.php):

    $curl = curl_init(); // Open a cURL library resource.
    
    // Set the resource option for the URL.
    curl_setopt($curl, CURLOPT_URL, "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download");
    // Set the resource option to let it return a value from the execution function.
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    $file = curl_exec($curl); // Execute cURL, load the result into variable $file.
    curl_close($curl); // Close the cURL resource.
    
    print_r($file); // The file should now be a string, exactly the CSV string that was scraped.
    

    【讨论】:

    • 我已经尝试使用带有 echo 的 fopen,但我只得到“Resource id #3”。我有什么遗漏的吗?
    • @PriscillaIp 很抱歉造成混乱。函数fopen() 返回一个文件资源,您必须对其进行操作才能获取字符串。在我编辑的答案中使用fread() 并回显该返回值,而不是资源。如果可以的话,我强烈建议您安装 cURL,因为您可以更好地控制安全性等。
    • 感谢 Stegrex 提供 fread(file,length)。我也想用cURL,但是我电脑上的库安装失败,很痛苦,我已经没时间了。
    • @PriscillaIp 使用 cURL 重新安装 PHP 可能会很痛苦,我同意。希望 fread() 解决方案解决了您的需求。
    【解决方案4】:

    您使用的是 C# 还是 PHP?我的解决方案是 C#。

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download");
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    StreamReader reader = new StreamReader(resp.GetResponseStream());
    
    string CSVContents = reader.ReadToEnd();
    

    CSVContents 现在包含文件的内容。

    【讨论】:

      【解决方案5】:

      这是一种通过 Stream 读取数据的方法,而不是将数据内容下载到您的物理磁盘。这是在 C# 中的(不知道你想要 PHP 还是 C#,因为你的标签包含两者)

      string uriString = @"http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download";
      WebClient myWebClient = new WebClient();
      Stream myStream = myWebClient.OpenRead(uriString);    //open a stream to the resource
      StreamReader sr = new StreamReader(myStream);
      Console.WriteLine(sr.ReadToEnd());                    //print contents of stream
      
      myStream.Close();
      

      【讨论】:

      • "没有实际下载文件" ...这有点误导。您仍然必须将数据从那里带到这里。仅仅因为它没有同时全部缓冲在内存中并不意味着您不会下载文件来获取它包含的数据。
      • @rdlowrey 感谢您的评论,我已将答案更新为(希望)更清晰。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多