dags att checka in
This commit is contained in:
9
WpfScraper/App.xaml
Normal file
9
WpfScraper/App.xaml
Normal file
@ -0,0 +1,9 @@
|
||||
<Application x:Class="WpfScraper.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:WpfScraper"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
17
WpfScraper/App.xaml.cs
Normal file
17
WpfScraper/App.xaml.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace WpfScraper
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
||||
10
WpfScraper/AssemblyInfo.cs
Normal file
10
WpfScraper/AssemblyInfo.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
14
WpfScraper/EntryModel.cs
Normal file
14
WpfScraper/EntryModel.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WpfScraper
|
||||
{
|
||||
public class EntryModel
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
43
WpfScraper/MainWindow.xaml
Normal file
43
WpfScraper/MainWindow.xaml
Normal file
@ -0,0 +1,43 @@
|
||||
<Window x:Class="WpfScraper.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:WpfScraper"
|
||||
mc:Ignorable="d"
|
||||
Title="My awesome webscraper" Height="450" Width="800">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="50"/>
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid Grid.Row="0">
|
||||
<DockPanel>
|
||||
<Menu>
|
||||
<MenuItem Header="File">
|
||||
<MenuItem Header="Export"
|
||||
x:Name="ItemExport"
|
||||
Click="ItemExport_Click"/>
|
||||
</MenuItem>
|
||||
</Menu>
|
||||
<Button x:Name="BtnScraper"
|
||||
Content="Scrape"
|
||||
DockPanel.Dock="Right"
|
||||
Height="25"
|
||||
Width="50"
|
||||
Click="BtnScraper_Click"
|
||||
Margin="0,0,10,0"/>
|
||||
|
||||
<TextBox x:Name="TbPage"
|
||||
DockPanel.Dock="Bottom"
|
||||
Text="WebPage..."
|
||||
Height="20"
|
||||
Margin="0,0,10,0"/>
|
||||
|
||||
</DockPanel>
|
||||
</Grid>
|
||||
<Grid Grid.Row="1">
|
||||
<DataGrid ItemsSource="{Binding Entries}"/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Window>
|
||||
41
WpfScraper/MainWindow.xaml.cs
Normal file
41
WpfScraper/MainWindow.xaml.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace WpfScraper
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
Scraper scraper;
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
scraper = new Scraper();
|
||||
DataContext = scraper;
|
||||
}
|
||||
|
||||
private void BtnScraper_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
scraper.ScrapeData(TbPage.Text);
|
||||
}
|
||||
|
||||
private void ItemExport_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
scraper.Export();
|
||||
}
|
||||
}
|
||||
}
|
||||
61
WpfScraper/Scraper.cs
Normal file
61
WpfScraper/Scraper.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using CsvHelper;
|
||||
using HtmlAgilityPack;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
namespace WpfScraper
|
||||
{
|
||||
public class Scraper
|
||||
{
|
||||
private ObservableCollection<EntryModel> _entries = new ObservableCollection<EntryModel>();
|
||||
|
||||
public ObservableCollection<EntryModel> Entries
|
||||
{
|
||||
get { return _entries; }
|
||||
set { _entries = value; }
|
||||
}
|
||||
|
||||
public void ScrapeData(string page)
|
||||
{
|
||||
var web = new HtmlWeb();
|
||||
var doc = web.Load(page);
|
||||
|
||||
//var Articles = doc.DocumentNode.SelectNodes("//*[@class = 'article-single']");
|
||||
var Articles = doc.DocumentNode.SelectNodes("//*[@id='shareslist']/table/tbody/tr[1]");
|
||||
|
||||
foreach (var article in Articles)
|
||||
{
|
||||
//var header = HttpUtility.HtmlDecode(article.SelectSingleNode(".//li[@class = 'article-header']").InnerText);
|
||||
//var description = HttpUtility.HtmlDecode(article.SelectSingleNode(".//li[@class = 'article-copy']").InnerText);
|
||||
var header = HttpUtility.HtmlDecode(article.SelectSingleNode(".//li[@class = 'millistream-link']").InnerText);
|
||||
var description = HttpUtility.HtmlDecode(article.SelectSingleNode(".//li[@class = 'millistream-numeric millistream-lastprice millistream-numeric-positive']").InnerText);
|
||||
Debug.Print($"Title: {header} \n"+
|
||||
$" Description: {description}");
|
||||
_entries.Add(new EntryModel { Title = header, Description = description });
|
||||
}
|
||||
}
|
||||
|
||||
public void Export()
|
||||
{
|
||||
using(TextWriter tw = File.CreateText("SampleData.csv"))
|
||||
{
|
||||
using(var cw = new CsvWriter(tw,CultureInfo.InvariantCulture))
|
||||
{
|
||||
foreach(var entry in Entries)
|
||||
{
|
||||
cw.WriteRecord(entry);
|
||||
cw.NextRecord();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
14
WpfScraper/WpfScraper.csproj
Normal file
14
WpfScraper/WpfScraper.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CsvHelper" Version="27.1.1" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.34" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user