using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using AliKeywordSearch; using System.Web; using CityCraft; namespace EnAliKeywordSearch { public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent(); } private void search_Click(object sender, EventArgs e) { search.Enabled = false; state.ForeColor = Color.Black; state.ForeColor = Color.Red; HttpHelper httpHelper = new HttpHelper(); 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(',')) { bool maybe = false; int maxpage = this.ToInt(max.Text); int index = this.ResultView.Rows.Add(); this.ResultView.FirstDisplayedScrollingRowIndex = index; this.ResultView.Rows[index].Cells["公司名称"].Value = cpy; this.ResultView.Rows[index].Cells["关键词"].Value = key; state.ForeColor = Color.Black; state.Text = "正在查询 " + cpy + " 的关键词 " + key; Application.DoEvents(); string pageinfo = string.Empty; 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", HttpUtility.UrlEncode(key), i); else url = String.Format("http://s.1688.com/selloffer/offer_search.htm?keywords={0}&beginPage={1}", HttpUtility.UrlEncode(key), i); httpHelper.Send(HttpMethod.GET, url); while (httpHelper.readyState != HttpReadyState.完成) { Application.DoEvents(); } string htmldoc = httpHelper.responseBody; if (string.IsNullOrEmpty(htmldoc)) { state.ForeColor = Color.Red; state.Text = "关键词 " + key + " 第 " + i + " 页 网页抓取失败 错误:" + httpHelper.ErrMsg; maybe = true; continue; } string encodecpy = HttpUtility.HtmlEncode(cpy); if (htmldoc.Contains(encodecpy)) { pageinfo = "第" + i + "页"; break; } if (i == maxpage) { pageinfo = maxpage + "页以后"; break; } Application.DoEvents(); } this.ResultView.Rows[index].Cells["排名"].Value = pageinfo + (maybe ? "(可能不准确)" : ""); this.ResultView.Rows[index].Cells["排名"].Style.ForeColor = maybe ? Color.Red : Color.Black; this.ResultView.Rows[index].Cells["查询时间"].Value = DateTime.Now.ToString(); Application.DoEvents(); } } Application.DoEvents(); } state.ForeColor = Color.Green; 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 } }