1
0
mirror of https://e.coding.net/circlecloud/AliKeywordSearch.git synced 2024-11-17 00:58:58 +00:00
AliKeywordSearch/Frm_Main.cs

168 lines
7.3 KiB
C#
Raw Normal View History

2015-07-25 10:07:54 +00:00
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;
2015-07-30 12:47:57 +00:00
using CityCraft;
2015-07-25 10:07:54 +00:00
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();
2015-07-25 10:07:54 +00:00
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;
2015-07-25 10:07:54 +00:00
int maxpage = this.ToInt(max.Text);
int index = this.ResultView.Rows.Add();
this.ResultView.FirstDisplayedScrollingRowIndex = index;
2015-07-25 10:07:54 +00:00
this.ResultView.Rows[index].Cells["公司名称"].Value = cpy;
this.ResultView.Rows[index].Cells["关键词"].Value = key;
state.ForeColor = Color.Black;
2015-07-25 10:07:54 +00:00
state.Text = "正在查询 " + cpy + " 的关键词 " + key;
Application.DoEvents();
string pageinfo = string.Empty;
2015-07-25 10:07:54 +00:00
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);
2015-07-25 10:07:54 +00:00
else
url = String.Format("http://s.1688.com/selloffer/offer_search.htm?keywords={0}&beginPage={1}", HttpUtility.UrlEncode(key), i);
2015-07-30 13:20:42 +00:00
for (int k = 1; k < int.Parse(retry.Text); k++)
2015-07-30 12:47:57 +00:00
{
2015-07-30 13:20:42 +00:00
httpHelper.Send(HttpMethod.GET, url);
while (httpHelper.readyState != HttpReadyState.)
{
Application.DoEvents();
}
if (!(string.IsNullOrEmpty(httpHelper.responseBody) || httpHelper.responseBody.StartsWith("0")))
break;
state.ForeColor = Color.Red;
state.Text = "关键词 " + key + " 第 " + i + " 页 网页抓取失败 重试第 " + k + " 次";
2015-07-30 12:47:57 +00:00
}
string htmldoc = httpHelper.responseBody;
2015-07-25 10:07:54 +00:00
if (string.IsNullOrEmpty(htmldoc))
{
state.ForeColor = Color.Red;
2015-07-30 12:47:57 +00:00
state.Text = "关键词 " + key + " 第 " + i + " 页 网页抓取失败 错误:" + httpHelper.ErrMsg;
maybe = true;
continue;
2015-07-25 10:07:54 +00:00
}
2015-07-27 09:13:22 +00:00
string encodecpy = HttpUtility.HtmlEncode(cpy);
if (htmldoc.Contains(encodecpy))
2015-07-25 10:07:54 +00:00
{
pageinfo = "第" + i + "页";
2015-07-25 10:07:54 +00:00
break;
}
if (i == maxpage)
{
pageinfo = maxpage + "页以后";
2015-07-25 10:07:54 +00:00
break;
}
Application.DoEvents();
}
this.ResultView.Rows[index].Cells["排名"].Value = pageinfo + (maybe ? "(可能不准确)" : "");
this.ResultView.Rows[index].Cells["排名"].Style.ForeColor = maybe ? Color.Red : Color.Black;
2015-07-25 10:07:54 +00:00
this.ResultView.Rows[index].Cells["查询时间"].Value = DateTime.Now.ToString();
Application.DoEvents();
}
}
Application.DoEvents();
}
state.ForeColor = Color.Green;
2015-07-25 10:07:54 +00:00
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
}
}