用F#抓取股票股息数据

时间:2016-03-27 14:08:53

标签: f# web-scraping f#-data

我正在尝试使用F#和FSharp.Data库从网页中获取股息股息数据。可以在http://www.nasdaq.com/symbol/ibm/dividend-history找到示例页面。

要请求网页,我的代码设置为一个简单的控制台应用程序作为示例,如下所示:

open FSharp.Data

[<EntryPoint>]
let main argv =
    let url = "http://www.nasdaq.com/symbol/ibm/dividend-history"
    let result = Http.RequestString(url)
    System.Console.ReadLine() |> ignore
    0 // return an integer exit code

运行时,RequestString方法错误:

“FSharp.Core.dll中发生了'System.ArgumentOutOfRangeException'类型的未处理异常

附加信息:长度不能小于零。“

看起来页面的格式化方式是“传统”的抓取方法不起作用。任何想法或想法将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是我运行代码时得到的完整堆栈跟踪:

System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length
   at System.String.Substring(Int32 startIndex, Int32 length)
   at FSharp.Data.HttpHelpers.getAllCookiesFromHeader@671.Invoke(Int32 i, String cookiePart) in C:\Git\FSharp.Data\src\Net\Http.fs:line 675
   at Microsoft.FSharp.Collections.ArrayModule.IterateIndexed[T](FSharpFunc`2 action, T[] array)
   at FSharp.Data.HttpHelpers.getAllCookiesFromHeader(String header, Uri responseUri, CookieContainer cookieContainer) in C:\Git\FSharp.Data\src\Net\Http.fs:line 671
   at <StartupCode$FSharp-Data>.$Http.InnerRequest@803-5.Invoke(WebResponse _arg2) in C:\Git\FSharp.Data\src\Net\Http.fs:line 803
   at Microsoft.FSharp.Control.AsyncBuilderImpl.args@835-1.Invoke(a a)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.FSharp.Control.AsyncBuilderImpl.commit[a](Result`1 res)
   at Microsoft.FSharp.Control.CancellationTokenOps.RunSynchronously[a](CancellationToken token, FSharpAsync`1 computation, FSharpOption`1 timeout)
>    at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T](FSharpAsync`1 computation, FSharpOption`1 timeout, FSharpOption`1 cancellationToken)
   at <StartupCode$FSI_0004>.$FSI_0004.main@() in C:\Users\helgeu.COMPODEAL\AppData\Local\Temp\~vs2B9.fsx:line 8
Stopped due to error

我认为您遗憾地发现了与此cookie处理代码相关的错误:

https://github.com/fsharp/FSharp.Data/issues/904

&LT;咆哮&GT;

我试图调查那段代码,但它让我头疼的是一些谷歌回答如何处理C#中的cookie,然后严重翻译成F#。

&LT; /咆哮&GT;

想想可能将信息添加到那个github案例可能是比这里更好的选择。