1
0
mirror of https://e.coding.net/circlecloud/AliKeywordSearch.git synced 2024-11-16 00:48:59 +00:00
AliKeywordSearch/Frm_Main.cs
2015-07-27 14:36:42 +08:00

153 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AliKeywordSearch;
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", 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))
{
state.ForeColor = Color.Red;
state.Text = "关键词 " + key + " 第 " + i + " 页 网页抓取失败 错误:" + HttpHelper.ErrMsg;
maybe = true;
continue;
}
if (htmldoc.Contains(cpy))
{
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<string> data = new List<string>();
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<string> 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
}
}