ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# selenium 下载某DJ音乐网音频

2021-02-21 15:02:24  阅读:146  来源: 互联网

标签:DJ string Title C# 音乐网 driver System new using


吾爱破解贴子地址: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; }
    }
}

标签:DJ,string,Title,C#,音乐网,driver,System,new,using
来源: https://blog.csdn.net/CGS_______/article/details/113917881

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有