2016-03-27 150 views
2

我試圖用F#和FSharp.Data庫從網頁刮取股票紅利數據。示例頁面可以在http://www.nasdaq.com/symbol/ibm/dividend-history處看到。F網刮股票分紅數據#

要請求的網頁,我的代碼是建立一個簡單的控制檯應用程序作爲一個實例,如下所示:

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'異常

附加信息:長度不能小於零。「

它看起來像頁面被格式化的方式,以「傳統」的刮取方法將無法正常工作。任何想法或想法,將不勝感激。

+0

我自己試了一下,發現:在'[email protected](Int32 i,String cookiePart)in ... FSharp.Data \ src \ Net \ Http.fs:line 675 '是實際墜機的地方。它試圖調用'String.Substring',這樣你就可以得到第一個「=」符號的長度。不幸的是,這是使用String.IndexOf定義的,當沒有「=」符號時它會給出-1。相關的源代碼:https://github.com/fsharp/FSharp.Data/blob/master/src/Net/Http.fs#L674和評論:'.NET在解析一些cookie時遇到困難。請參閱http:// stackoverflow.com/a/22098131/165633 – Ringil

回答

0

這是一個完整的堆棧跟蹤我得到的,當我運行代碼:

System.ArgumentOutOfRangeException: Length cannot be less than zero. 
Parameter name: length 
    at System.String.Substring(Int32 startIndex, Int32 length) 
    at [email protected](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>[email protected](WebResponse _arg2) in C:\Git\FSharp.Data\src\Net\Http.fs:line 803 
    at [email protected](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>[email protected]() 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

<咆哮>

我試圖去看看那個代碼,但它讓我頭疼從一些谷歌的惡意剪切和粘貼如何在C#中處理cookie,然後嚴重轉換爲F#的答案。

< /咆哮>

想也許添加信息到GitHub的情況下,可能會比這裏更好的選擇。