2014-03-01 69 views
0

這是我的要求。有一個公共網站,它將字母數字字符串作爲輸入,並將數據檢索到表格元素中(通過按鈕單擊)。表格元素有幾個標籤,它們會填充相應的數據。我需要一個可以檢查網站數據庫中是否存在特定字符串的工具/解決方案。如果是,則檢索該字符串的所有出現的所有ID。查看網站的「查看源代碼」(沒有使用JavaScript),我注意到輸入元素名稱和按鈕元素名稱,並藉助現有樣本,我得到了一個可行的解決方案。下面是可用的代碼,但我想檢查是否有更好更快的方法。我知道下面的代碼有一些問題,如「無限循環」問題和其他問題。但我基本上正在尋找可以快速工作一百萬條記錄的備用解決方案。C#中更好的方法在第三方網站上搜索數據

namespace SearchWebSite 
    { 
     public partial class Form1 : Form 
     { 
      bool searched = false; 
      long i; 

      public Form1() 
      { 
       InitializeComponent(); 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       i = 1; 
       WebBrowser browser = new WebBrowser(); 
       string target = "http://www.SomePublicWebsite.com"; 
       browser.Navigate(target); 
       browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(XYZ); 
      } 


      private void XYZ(object sender, WebBrowserDocumentCompletedEventArgs e) 
      { 
       WebBrowser b = null; 
       if (searched == false) 
       { 
        b = (WebBrowser)sender; 
        b.Document.GetElementById("txtId").InnerText = "M" + i.ToString(); 
        b.Document.GetElementById("btnSearch").InvokeMember("click"); 
        searched = true; 
       } 

       if (b.ReadyState == WebBrowserReadyState.Complete) 
       { 
        if (b.Document.GetElementById("lblName") != null) 
        { 
         string IdNo = "M" + i.ToString(); 
         string DateString = b.Document.GetElementById("lblDate").InnerHtml; 
         string NameString = b.Document.GetElementById("lblName").InnerHtml; 

         if (NameString != null && (NameString.Contains("XXXX") || NameString.Contains("xxxx"))) 
         { 
          using (StreamWriter w = File.AppendText("log.txt")) 
          { 
           w.WriteLine("Id {0}, Date {1}, Name {2}", IdNo, DateString, NameString); 
           i = i + 1; 
           searched = false; 
          } 
         } 
         else 
         { 
          i = i + 1; 
          searched = false; 
         } 
        } 
        else 
        { 
         i = i + 1; 
         searched = false; 
        } 
       } 
      } 
     } 
    } 
+0

'i = i + 1'。有趣:-) –

+1

這絕對是更適合[代碼評論](http://codereview.stackexchange.com/) – Jonesopolis

+2

也爲什麼你關心無限循環?我甚至沒有看到一個循環 – Jonesopolis

回答

0

如果SEACH按鈕後,頁面點擊包含txtId和btnSearch控制比你可以使用此代碼段,這是不是快,但正確的形式,我認爲。

public partial class Form1 : Form 
{ 
    bool searched = false; 
    long i = 1; 
    private string IdNo { get { return "M" + i.ToString(); } } 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     i = 1; 
     WebBrowser browser = new WebBrowser(); 
     string target = "http://www.SomePublicWebsite.com"; 
     browser.Navigate(target); 
     browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(XYZ); 
    } 
    private void XYZ(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     WebBrowser b = (WebBrowser)sender; 
     if (b.ReadyState == WebBrowserReadyState. Complete) 
     { 
      if (searched == false) 
      { 
       DoSearch(b); return; 
      } 
      if (b.Document.GetElementById("lblName") != null) 
      { 
       string DateString = b.Document.GetElementById("lblDate").InnerHtml; 
       string NameString = b.Document.GetElementById("lblName").InnerHtml; 

       if (NameString != null && (NameString.Contains("XXXX") || NameString.Contains("xxxx"))) 
        using (StreamWriter w = File.AppendText("log.txt")) 
         w.WriteLine("Id {0}, Date {1}, Name {2}", IdNo, DateString, NameString); 
      } 
      i++; 
      DoSearch(b); 
     } 
    } 
    private void DoSearch(WebBrowser wb) 
    { 
     wb.Document.GetElementById("txtId").InnerText = IdNo; 
     wb.Document.GetElementById("btnSearch").InvokeMember("click"); 
     searched = true; 
    } 
}