Skip to content

Instantly share code, notes, and snippets.

@sheepla
Last active December 31, 2023 13:03
Show Gist options
  • Save sheepla/3167e5c652749649a0970c364cb3e36d to your computer and use it in GitHub Desktop.
Save sheepla/3167e5c652749649a0970c364cb3e36d to your computer and use it in GitHub Desktop.
Get DuckDuckGo search result with F# using AngleSharp
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
@sheepla
Copy link
Author

sheepla commented Dec 31, 2023

AngleSharp便利!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment