吾爱破解贴子地址:https://www.52pojie.cn/thread-1374014-1-1.html
包涵的知识点:
- selenium需要的webdriver的选型
- 配置webdriver环境变量
- winform操作html标签与执行javaScript代码
- 使用Task、委托更新UI线程
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.Forms;
using HZH_Controls.Controls;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using System.IO;
using System.Net;
using System.Threading;
namespace BabyMusicDownloader
{
public partial class Form1 : FrmWithTitle
{
public Form1()
{
InitializeComponent();
m_SyncContext = SynchronizationContext.Current;
this.Title = "【吾爱破解论坛】 MusicDownloadApp - by Pojie1999.0909";
this.IsShowCloseBtn = true;
List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Title", HeadText = "标题", Width = 40, WidthType = SizeType.Percent });
lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Href", HeadText = "链接", Width = 40, WidthType = SizeType.Percent });
lstCulumns.Add(new DataGridViewColumnEntity()
{
DataField = "State",
HeadText = "状态",
Width = 20,
WidthType = SizeType.Percent,
Format = (a) => { return Convert.ToInt32(a) == 0 ? "等待下载" : Convert.ToInt32(a) == 1 ? "下载完成" : "歌曲已存在"; }
});
this.ucDataGridView1.Columns = lstCulumns;
this.ucDataGridView1.IsShowCheckBox = true;
}
private void ucBtnExt1_BtnClick(object sender, EventArgs e)
{
lstSource.Clear();
driver.Navigate().GoToUrl(ucTextBoxEx1.InputText);
IWebElement table = driver.FindElement(By.ClassName("list_musiclist"));
IList<IWebElement> redlines = table.FindElements(By.ClassName("redline"));
foreach (IWebElement item in redlines)
{
string song = item.FindElement(By.TagName("a")).Text;
lstSource.Add(new SongObj { Title = song + ".mp4", Href = item.FindElement(By.TagName("a")).GetAttribute("href"), State = "0" });
}
ucDataGridView1.DataSource = lstSource;
}
private void ucBtnExt2_BtnClick(object sender, EventArgs e)
{
Task.Run(() =>
{
ThreadProcSafePost();
});
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
driver.Close();
driver.Quit();
}
private void ThreadProcSafePost()
{
if (ucDataGridView1.SelectRows.Count < 1)
{
FrmDialog.ShowDialog(this, "请选择至少一条记录!", "提示");
}
else
{
foreach (var item in ucDataGridView1.SelectRows)
{
string title = ((SongObj)item.DataSource).Title;
string href = ((SongObj)item.DataSource).Href;
driver.Navigate().GoToUrl(href);
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string truthUrl = js.ExecuteScript("return playurl2").ToString();
string fileName = rootPath + title;
if (!File.Exists(fileName))
{
WebClient client = new WebClient();
client.DownloadFile(truthUrl, fileName);
lstSource.Where(x => x.Title == title && x.Href == href).ToList()[0].State = "1";
}
else
{
lstSource.Where(x => x.Title == title && x.Href == href).ToList()[0].State = "2";
}
m_SyncContext.Post(SetDataGridViewSafePost, null);
}
}
}
private void SetDataGridViewSafePost(object state)
{
this.ucDataGridView1.ReloadSource();
}
IWebDriver driver = new EdgeDriver();
const string domain = "http://www.abc.com/";
const string rootPath = @"D:\";
List<SongObj> lstSource = new List<SongObj>();
SynchronizationContext m_SyncContext = null;
}
public class SongObj
{
public string Title { get; set; }
public string Href { get; set; }
public string State { get; set; }
}
}