using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using AliKeywordSearch; namespace EnAliKeywordSearch { public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent(); backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged; backgroundWorker.DoWork += backgroundWorker_DoWork; backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; } private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { } private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { } void DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = sender as BackgroundWorker; } private void search_Click(object sender, EventArgs e) { search.Enabled = false; foreach (DataGridViewRow item in this.SearchView.Rows) { string cpy = item.Cells["company"].Value == null ? "" : item.Cells["company"].Value.ToString(); string keys = item.Cells["keyword"].Value == null ? "" : item.Cells["keyword"].Value.ToString(); if (cpy != "" && keys != "") { foreach (string key in keys.Split(',')) { int maxpage = this.ToInt(max.Text); int index = this.ResultView.Rows.Add(); this.ResultView.Rows[index].Cells["公司名称"].Value = cpy; this.ResultView.Rows[index].Cells["关键词"].Value = key; state.Text = "正在查询 " + cpy + " 的关键词 " + key; Application.DoEvents(); for (int i = 1; i <= maxpage; i++) { string url = string.Empty; this.ResultView.Rows[index].Cells["排名"].Value = "正在查询第" + i + "页..."; Application.DoEvents(); if (enAli.Checked == true) url = String.Format("http://www.alibaba.com/products/F0/{0}/{1}.html", key, i); else url = String.Format("http://s.1688.com/selloffer/offer_search.htm?keywords={0}&beginPage={1}", key, i); string htmldoc = HttpHelper.Get(url); if (string.IsNullOrEmpty(htmldoc)) { this.ResultView.Rows[index].Cells["排名"].Value = "网页抓取失败"; break; } if (htmldoc.Contains(cpy)) { this.ResultView.Rows[index].Cells["排名"].Value = "第" + i + "页"; break; } if (i == maxpage) { this.ResultView.Rows[index].Cells["排名"].Value = maxpage + "页以后"; break; } Application.DoEvents(); } this.ResultView.Rows[index].Cells["查询时间"].Value = DateTime.Now.ToString(); Application.DoEvents(); } } Application.DoEvents(); } state.Text = "所有关键词查询完成!"; search.Enabled = true; } private int ToInt(String str) { int s = 0; int.TryParse(str, out s); return s; } private void Frm_Main_Load(object sender, EventArgs e) { } #region 窗体事件 private void saveout_Click(object sender, EventArgs e) { SaveFileDialog cpm = new SaveFileDialog(); cpm.Filter = "查排名数据|*.cpm"; if (cpm.ShowDialog() == DialogResult.OK) { List data = new List(); foreach (DataGridViewRow item in this.SearchView.Rows) { string cpy = item.Cells["company"].Value == null ? "" : item.Cells["company"].Value.ToString(); string keys = item.Cells["keyword"].Value == null ? "" : item.Cells["keyword"].Value.ToString(); if (cpy != "" && keys != "") data.Add(cpy + "|" + keys); } Config cfg = new Config(cpm.FileName); cfg.WriteListToTextFile(data); } } private void readin_Click(object sender, EventArgs e) { OpenFileDialog cpm = new OpenFileDialog(); cpm.Filter = "查排名数据|*.cpm"; if (cpm.ShowDialog() == DialogResult.OK) { Config cfg = new Config(cpm.FileName); List data = cfg.ReadTextFileToList(); string cpy = string.Empty; string keys = string.Empty; foreach (string item in data) { string[] str = item.Split('|'); if (str.Length == 2) { cpy = str[0]; keys = str[1]; int index = this.SearchView.Rows.Add(); this.SearchView.Rows[index].Cells["company"].Value = cpy; this.SearchView.Rows[index].Cells["keyword"].Value = keys; } } } } private void clearresult_Click(object sender, EventArgs e) { ResultView.Rows.Clear(); } private void clearsearch_Click(object sender, EventArgs e) { SearchView.Rows.Clear(); } #endregion } }