Skip to content

Instantly share code, notes, and snippets.

@Igouist
Last active June 4, 2022 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Igouist/039148b1aaaa8e3073f5e135ff689f9b to your computer and use it in GitHub Desktop.
Save Igouist/039148b1aaaa8e3073f5e135ff689f9b to your computer and use it in GitHub Desktop.
使用 AngleSharp + Yahoo 股市來抓取目標股票的當日資訊
async Task Main()
{
var key = "2330";
var url = $"https://tw.stock.yahoo.com/quote/{key}";
var config = AngleSharp.Configuration.Default.WithDefaultLoader(
new LoaderOptions
{
IsResourceLoadingEnabled = true
});
var browser = BrowsingContext.New(config);
var document = await browser.OpenAsync(url);
var tables = document?.QuerySelectorAll("li.price-detail-item");
document.Close();
tables.ToDictionary(
t => t.FirstChild.TextContent, // 欄位名稱 ex: 開盤價
t => t.LastChild.TextContent) // 欄位數值 ex: 500
.Dump();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment