c#抓取网页数据(2.Python构建自动在线刷视频——一个)
优采云 发布时间: 2022-01-07 21:04c#抓取网页数据(2.Python构建自动在线刷视频——一个)
文章内容
我以前写过两个相关的博客:
1.C#make 网站挂机程序:
()
2.Python构建自动在线视频刷机——一个只能做不能说的项目:
()
3.【C#制作hook程序V2.0——鼠标滑动点击等在线视频程序】下载链接:
()
第一篇文章主要教大家如何使用C#制作挂机程序。代码比较简单,主要是完成一个简单的功能,可以处理网页中弹出的警告对话框。第二篇文章采用Python语言,完全控制浏览器,可以抓取网页中的Tag、id、name或CSS等标签,输入键盘鼠标。它应该是一个完美的程序。但是今天,为什么又要回到C#开发的老路,让键盘鼠标点击事件自动挂断呢?Python不香吗?代码不禁呵呵,Python的坑不多,反爬技术直接让Python驱动浏览器拜拜了。
我们直接送干货
一、程序界面
程序相当简单,但不要小看它,功能不小!
二、功能说明1.程序集成了Microsoft Spy++的功能2.通过拖拽工具自动获取表单标题3.拖拽工具自动获取鼠标就绪到点击点4.通过时间设置点击事件后的等待时间(视频刷新需要)5.浏览器应用不再局限于FireFox6.适应性增加,只要它是通过鼠标点击完成的操作就可以交给它了。7.表单程序的自动点击也适用
需要强调的是,拖动工具获取坐标和表格标题的方法可能只能在C#开发中实现。因为码农搜索了百度也没有找到,所以有这个功能的都是用C++开发的。所以这个想法完全是 C# 应用程序独有的。
三、程序步骤
核心功能展示:
1.程序运行后,点击【获取浏览器标题:】,会出现以下工具:
2. 将放大镜工具拖到对应的程序窗体上,会自动获取窗体的标题
该函数的主要目的是获取应用程序的句柄。借助手柄,可以将表格设置为顶部显示。(不被其他窗口覆盖)
3. 点击【获取坐标】按钮,会出现以下工具:
4. 将放大镜工具拖到第一个需要自动点击的点,松开,弹出如下窗口:
坐标不需要设置,就是刚才松开鼠标的位置,暂停时间就是点击后停止的时间,以及下一次操作的时间。
5.循环3、4运行,可以得到一系列坐标点和对应的暂停时间6.所有设置完成后,直接点击【开始】按钮开始自动运行。7.注意,这个自动化操作会反复进行。您可以直接关闭此程序以关闭此自动操作。也可以点击【保存坐标】将坐标点和时间保存到文件中,重启时自动加载已有的坐标点和时间。三、程序密钥代码1.API参考和成员变量
#region API及成员变量
///
/// 根据坐标获取窗口句柄
///
/// 坐标
///
[DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point point);
public delegate bool EnumChildWindow(IntPtr WindowHandle, string num);
///
/// 传递消息给记事本
///
///
///
///
///
///
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(int hWnd);
[DllImport("User32.dll")]
public static extern int EnumChildWindows(IntPtr WinHandle, EnumChildWindow ecw, string name);
[DllImport("User32.dll")]
public static extern int GetWindowText(IntPtr WinHandle, StringBuilder Title, int size);
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr WinHandle, StringBuilder Type, int size);
[DllImport("user32")]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int pid);
[DllImport("user32")]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
//查找窗体控件
public int iSecond = 30;
public delegate bool CallBack(int hwnd, int lParam);
public RECT rectMain = new RECT();
private string typeName;
private IntPtr mainHwnd;
public IntPtr ip;
private string BSType = "Chrome_WidgetWin_1";
bool Flag = false;
int X;
int Y;
int times;
private IntPtr mainWindowHandle;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int X; //最左坐标
public int Y; //最上坐标
public int Height; //最右坐标
public int Width; //最下坐标
}
///
/// 查找句柄
///
///
///
///
///
///
[DllImport("User32.DLL")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32")]
public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public const uint WM_SETTEXT = 0x000C;
public System.Diagnostics.Process Proc;
public System.Windows.Forms.Timer myTimer;
public List optList = new List();
public Queue optQueue = new Queue();
#endregion
2.移动鼠标代码
public void MoveTo(int x1, int y1, int x2, int y2)
{
float k = (float)(y2 - y1) / (float)(x2 - x1);
float b = y2 - k * x2;
for (int x = x2; x != x1; x = x + Math.Sign(x1 - x2))
{
//MoveTo(x1,y1,x,(k*x+b));
SetCursorPos(x, (int)(k * x + b));
Thread.Sleep(3);
}
}
代码如下(示例):
3.开始事件代码
private void btnStart_Click(object sender, EventArgs e)
{
foreach (string strLine in richTextBox1.Lines)
{
string[] strInt = strLine.Split(new string[] { "," }, StringSplitOptions.None);
if (strInt.Length 0)
{
Opt opt = optQueue.Dequeue();
X = opt.x;
Y = opt.y;
times = opt.Times;
System.Timers.Timer t = new System.Timers.Timer();//实例化
t.Elapsed += new System.Timers.ElapsedEventHandler(CallBack2);
t.AutoReset = false;
t.Interval = 1000 * times;
t.Enabled = true;
}
}
4.定时器事件代码
private void CallBack2(object sender, EventArgs e)
{
MoveTo(X, Y, MousePosition.X, MousePosition.Y);
mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), X, Y, 0, IntPtr.Zero);
//Thread.Sleep(200);
mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), X, Y, 0, IntPtr.Zero);
if (optQueue.Count > 0)
{
Opt opt = optQueue.Dequeue();
X = opt.x;
Y = opt.y;
times = opt.Times;
System.Timers.Timer t = new System.Timers.Timer();//实例化
t.Elapsed += new System.Timers.ElapsedEventHandler(CallBack2);
t.AutoReset = false;
t.Interval = 1000 * times;
t.Enabled = true;
}
else
{
foreach (Opt opt1 in optList)
{
optQueue.Enqueue(opt1);
}
Opt opt = optQueue.Dequeue();
X = opt.x;
Y = opt.y;
times = opt.Times;
System.Timers.Timer t = new System.Timers.Timer();//实例化
t.Elapsed += new System.Timers.ElapsedEventHandler(CallBack2);
t.AutoReset = false;
t.Interval = 1000 * times;
t.Enabled = true;
}
}
5.启动浏览器事件代码
private void btnStartBrowser_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFile.Text)) return;
try
{
// 浏览器程序启动线程
Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = txtFile.Text;
Proc.StartInfo.Arguments = txtNetAddr.Text; //浏览器打开URL参数
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.RedirectStandardInput = true;
Proc.StartInfo.RedirectStandardOutput = true;
Proc.Start();
}
catch
{
Proc = null;
}
}
总结
挂机程序本身的开发存在局限性。通常仅用于特定或特定类型的应用程序。但只要注意思考和挖掘,无论是哪种应用,总能找到对应的点,开发对应的挂机程序。用一个hook程序吃遍全世界是不可行的。