Created
October 30, 2024 19:30
-
-
Save swharden/0b27e97255ec50461d6ab682829e454b to your computer and use it in GitHub Desktop.
QRZ Logbook Scraping with C#
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
/* This C# .NET console application demonstrates how to log in to QRZ.com | |
* and scrape information from the logbook. Tested/working in October 2024. | |
* | |
* To run this code: | |
* Install the latest .NET SDK https://dotnet.microsoft.com/ | |
* Create an empty folder and run 'dotnet new console' | |
* Edit Program.cs to contain the following | |
* Use the 'dotnet run' command to execute the program | |
*/ | |
using HtmlAgilityPack; | |
using System.Net; | |
HttpClientHandler handler = new() { CookieContainer = new CookieContainer() }; | |
HttpClient client = new(handler) { BaseAddress = new Uri("https://www.qrz.com"), }; | |
FormUrlEncodedContent loginData = new( | |
[ | |
new KeyValuePair<string, string>("username", "my_callsign"), // MODIFY THIS LINE | |
new KeyValuePair<string, string>("password", "my_password"), // MODIFY THIS LINE | |
]); | |
var loginResponse = await client.PostAsync("https://www.qrz.com/login", loginData); | |
var subsequentResponse = await client.GetAsync("http://logbook.qrz.com"); | |
string html = await subsequentResponse.Content.ReadAsStringAsync(); | |
ParseLogbookPage(html); | |
static void ParseLogbookPage(string html) | |
{ | |
var doc = new HtmlDocument(); | |
doc.LoadHtml(html); | |
var table = doc.GetElementbyId("lbtab"); | |
foreach (var row in table.SelectNodes(".//tr[contains(@class, 'lrow')]")) | |
{ | |
DateTime date = DateTime.Parse(row.SelectSingleNode(".//td[contains(@class, 'td_date')]").GetAttributeValue("title", null)); | |
string callsign = row.SelectSingleNode(".//td[contains(@class, 'td_call2')]").InnerText.Trim().Replace("Ø", "0"); | |
string band = row.SelectSingleNode(".//td[contains(@class, 'td_band1')]").InnerText.Trim(); | |
Console.WriteLine($"{date}\t{callsign}\t{band}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment