在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、最終效果