Last active
December 31, 2023 13:03
-
-
Save sheepla/3167e5c652749649a0970c364cb3e36d to your computer and use it in GitHub Desktop.
Get DuckDuckGo search result with F# using AngleSharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Web | |
open AngleSharp | |
open FSharp.Json | |
type SearchResult = | |
{ | |
Title: string | |
Link: string | |
Snippet: string | |
} | |
let search (searchQuery: string) = | |
let url = | |
let builder = new UriBuilder("https://html.duckduckgo.com/html") | |
builder.Query <- | |
let q = HttpUtility.ParseQueryString("") | |
q.Add("q", searchQuery) | |
q.Add("v", "1") | |
q.Add("o", "json") | |
q.Add("api", "/d.js") | |
q.ToString() | |
builder.ToString() | |
let ctx = BrowsingContext.New(Configuration.Default.WithDefaultLoader()) | |
let doc = ctx.OpenAsync(url).GetAwaiter().GetResult() | |
doc.QuerySelectorAll(".result") | |
|> Seq.map (fun item -> | |
{ | |
Title = item.QuerySelector(".result__title").TextContent.Trim() | |
Link = | |
let href = | |
item.QuerySelector("a.result__url").GetAttribute("href") | |
let q = HttpUtility.ParseQueryString <| Uri(href).Query | |
q.Get("uddg") | |
Snippet = item.QuerySelector(".result__snippet").TextContent.Trim() | |
} | |
) | |
[<EntryPoint>] | |
let main argv = | |
let result = search argv[0] | |
result |> printfn "%A" | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AngleSharp便利!