C# winform 配合python实现网络爬虫抓取网络签名实例

在B站网上学习视频,看到一些关于python的网络爬虫方面的gui软件开发,实现提交请求,然后返回图片的签名,个人感他的界面设计没有像C#,winform那样方便设计。

所以我就在想能不能爬虫方面用python来实现,界面方面使用C#来做。有这个想就得立马行动。不然就只能是空想。

下面把我实现的步骤写出来,供小白借鉴和学习使用。欢迎各个高手批评指教。

1、首先使用python语言编写爬虫抓签名网的图片地址

__author__ = 'Administrator'

import requests as rq
import re
import  sys
url="http://www.uustv.com/"
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
name=sys.argv[1]
fnts=sys.argv[2]
ziti={
'艺术':'1.ttf',
'连笔':'zql.ttf',
'商务':"8.ttf",
'楷书':'6.ttf',
'潇洒':'bzcs.ttf',
'草体':'lfc.ttf',
'行书':'2.ttf',
'个性':'3.ttf',
'可爱':'yqk.ttf'
}
fnts=ziti[fnts]
data={
'word': name,
'sizes': 60,
'fonts': fnts,
'fontcolor':'#FF0000'
}
'''<option value="1.ttf" >艺术签</option>
		<option value="zql.ttf" selected="selected">连笔签</option>
		<option value="8.ttf" >商务签</option>
		<option value="6.ttf" >楷书签</option>
		<option value="bzcs.ttf" >潇洒签</option>
		<option value="lfc.ttf" >草体签</option>
		<option value="2.ttf" >行书签</option>
		<option value="3.ttf" >个性签</option>
		<option value="yqk.ttf" >可爱签</option>
		'''
resualt=rq.post(url=url,headers=headers,data=data)
resualt.encoding='utf-8'
a = re.findall('<img src="(.*?)"/>', resualt.text,re.S)   #第二步,调用模块函数
print(url+a[0])  #以列表形式返回匹配到的字符串
getpic=rq.get(url+a[0])
#<div class="tu"><img src="tmp/159759548773182.gif"/></div>
with open("1.gif","wb") as file:
	file.write(getpic.content)

2、用pyinstaller把py脚本编译成exe可执行文件。

如:pyinstaller -F test.py

3、设计如下的C#,winform界面。

 1 using System;
 2 using System.Diagnostics;
 3 using System.IO;
 4 using System.Windows.Forms;
 5 
 6 namespace 艺术签名
 7 {
 8     public partial class Form1 : Form
 9     {
10         public Form1()
11         {
12             InitializeComponent();
13         }
14 
15         private void Form1_Load(object sender, EventArgs e)
16         {
17             comboBox1.SelectedIndex = 0;
18         }
19 
20         private void button1_Click(object sender, EventArgs e)
21         {
22             try
23             {
24                 if (File.Exists("1.gif"))
25                 {
26                     File.Delete("1.gif");
27                 }
28                 if (!textBox1.Text.Trim().Equals(string.Empty))
29                 {
30                     string txtname = textBox1.Text;
31                     string ph = Application.StartupPath;
32                     Process p = new Process();
33                     p.StartInfo.FileName = ph + "\\test1.exe";//需要执行的文件路径
34                     p.StartInfo.UseShellExecute = false; //必需
35                     p.StartInfo.RedirectStandardOutput = true;//输出参数设定
36                     p.StartInfo.RedirectStandardInput = true;//传入参数设定
37                     p.StartInfo.CreateNoWindow = true;
38                     p.StartInfo.Arguments = txtname + $" {comboBox1.SelectedItem.ToString()}";//参数以空格分隔,如果某个参数为空,可以传入””
39                     p.Start();
40                     string output = p.StandardOutput.ReadToEnd();
41                     p.WaitForExit();//关键,等待外部程序退出后才能往下执行}
42                     Console.Write(output);//输出;
43                     p.Close();
44                     UriBuilder url = new UriBuilder(output);
45                     Console.WriteLine(url.Uri.ToString());
46                     webBrowser1.Url = url.Uri;
47                 }
48                 else
49                 {
50                     MessageBox.Show("请输入您的签名哦!","提示",  MessageBoxButtons.OK, MessageBoxIcon.Information);
51                 }
52             }
53             catch (Exception err)
54             {
55                 MessageBox.Show("报错提示", err.Message, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
56             }
57         }
58 
59         private void label1_Click(object sender, EventArgs e)
60         {
61             Console.WriteLine(comboBox1.SelectedItem.ToString());
62         }
63     }
64 }

4、最终效果

posted @ 2020-08-17 11:13  星空沃土  阅读(1153)  评论(0编辑  收藏  举报