private void cmdSpider_Click(object sender, EventArgs e) { OleDbConnection contentConnection; OleDbConnection marketConnection; try { //this.txtContentCN.Text = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\Users\\M.S.cxc\\Documents\\工作\\SpiderResult\\SpiderResult.mdb"; //this.txtMarketCN.Text = "Data Source=127.0.0.1;Initial Catalog=database;Persist Security Info=True;User ID=sa;Password=sa"; contentConnection = new OleDbConnection(this.txtContentCN.Text); marketConnection = new OleDbConnection(this.txtMarketCN.Text); } catch (Exception err) { if (this.checkIsThrow.Checked) { throw err; } else { MessageBox.Show(err.Message); return; } } OleDbCommand getIDCommand = new OleDbCommand("select ID from content where 已发 = 0", contentConnection); OleDbCommand getContentCommand = new OleDbCommand("select 标题 as Title, 内容 as Content, 时间 as PublishDate from content where ID = @id", contentConnection); getContentCommand.Parameters.Add("@id", OleDbType.BigInt); OleDbCommand setContentCommand = new OleDbCommand("update content set result = @result, 已发 = @ok where ID = @id", contentConnection); setContentCommand.Parameters.Add("@result", OleDbType.VarWChar, 255); setContentCommand.Parameters.Add("@ok", OleDbType.Boolean); setContentCommand.Parameters.Add("@id", OleDbType.BigInt); OleDbCommand addMarketCommand = new OleDbCommand("INSERT INTO Market (SteelType, DeliverySpot, PublishDate, SteelVariety, SteelSpec, SteelQuality, ProducingArea, Price, Range, Remark) VALUES (@steelType, @deliverySpot, @publishDate, @steelVariety, @steelSpec, @steelQuality, @producingArea, @price, @range, @remark)", marketConnection); addMarketCommand.Parameters.Add("@steelType", OleDbType.VarWChar, 50); addMarketCommand.Parameters.Add("@deliverySpot", OleDbType.VarWChar, 50); addMarketCommand.Parameters.Add("@publishDate", OleDbType.Date); addMarketCommand.Parameters.Add("@steelVariety", OleDbType.VarWChar, 50); addMarketCommand.Parameters.Add("@steelSpec", OleDbType.VarWChar, 50); addMarketCommand.Parameters.Add("@steelQuality", OleDbType.VarWChar, 50); addMarketCommand.Parameters.Add("@producingArea", OleDbType.VarWChar, 50); addMarketCommand.Parameters.Add("@price", OleDbType.VarWChar, 50); addMarketCommand.Parameters.Add("@range", OleDbType.VarWChar, 50); addMarketCommand.Parameters.Add("@remark", OleDbType.VarWChar, 50); this.isStop = false; try { contentConnection.Open(); OleDbDataReader idReader = getIDCommand.ExecuteReader(); int contentCount = 0; int marketCount = 0; while (idReader.Read()) { getContentCommand.Parameters["@id"].Value = idReader["ID"]; OleDbDataReader contentReader = getContentCommand.ExecuteReader(); if (!contentReader.Read()) { continue; } if (contentReader.IsDBNull(0) || contentReader.IsDBNull(1) || contentReader.IsDBNull(2)) { continue; } SortedList <int, string> columns; List <List <string> > valueLists; string deliverySpot; string steelType; DateTime publishDate; string result = this.analyse(contentReader["Title"].ToString(), contentReader["PublishDate"].ToString(), string.Format("'{0}'", IEBrowser.EscapeCharacter(contentReader["Content"].ToString()).Replace("'", "\\'")), out deliverySpot, out steelType, out publishDate, out columns, out valueLists); contentReader.Close(); marketConnection.Open(); if (result == string.Empty) { foreach (List <string> values in valueLists) { addMarketCommand.Parameters["@steelType"].Value = steelType; addMarketCommand.Parameters["@deliverySpot"].Value = deliverySpot; addMarketCommand.Parameters["@publishDate"].Value = publishDate; addMarketCommand.Parameters["@steelVariety"].Value = values[columns.IndexOfValue("sv")]; addMarketCommand.Parameters["@steelSpec"].Value = values[columns.IndexOfValue("ss")]; addMarketCommand.Parameters["@steelQuality"].Value = values[columns.IndexOfValue("sq")]; addMarketCommand.Parameters["@producingArea"].Value = values[columns.IndexOfValue("pa")]; addMarketCommand.Parameters["@price"].Value = values[columns.IndexOfValue("p")]; addMarketCommand.Parameters["@range"].Value = values[columns.IndexOfValue("r")]; addMarketCommand.Parameters["@remark"].Value = values[columns.IndexOfValue("re")]; addMarketCommand.ExecuteNonQuery(); marketCount++; Application.DoEvents(); } } marketConnection.Close(); setContentCommand.Parameters["@result"].Value = result; setContentCommand.Parameters["@ok"].Value = (result == string.Empty); setContentCommand.Parameters["@id"].Value = idReader["ID"]; setContentCommand.ExecuteNonQuery(); Application.DoEvents(); this.lblInfo.Text = string.Format("共分析 {0} 个页面, 导入 {1} 条行情", ++contentCount, marketCount); if (this.isStop) { break; } } this.lblInfo.Text = string.Format("已经停止或完毕, 共分析 {0} 个页面, 导入 {1} 条行情", ++contentCount, marketCount); idReader.Close(); } catch (Exception err) { if (this.checkIsThrow.Checked) { throw err; } else { MessageBox.Show(err.Message); return; } } finally { contentConnection.Close(); marketConnection.Close(); } }
private void cmd163Blog_Click(object sender, EventArgs e) { // 从当前的 WebBrowser 控件创建 IEBrowser 对象. IEBrowser ie = new IEBrowser(this.webBrowser); // 此处修改为您的 163 博客地址. ie.Navigate("http://<163 博客地址>"); ie.IEFlow.Wait(3); // 安装 jquery 脚本. ie.InstallJQuery(Properties.Resources.jquery_1_5_2_min); // 弹出登录框. ie.ExecuteJQuery(JQuery.Create("'a:contains(登录)'").Attr("'id'", "'denglu'")); ie.ExecuteScript("document.getElementById('denglu').click();"); // 填写用户信息并登录. ie.ExecuteJQuery(JQuery.Create("'.ztxt:text'").Val("'<用户名>'")); ie.ExecuteJQuery(JQuery.Create("'.ztxt:password'").Val("'<密码>'")); ie.ExecuteJQuery(JQuery.Create("'.wbtnok:button'").Attr("'id'", "'dl'")); ie.ExecuteScript("document.getElementById('dl').click();"); ie.IEFlow.Wait(5); // 安装 jquery 脚本. ie.InstallJQuery(Properties.Resources.jquery_1_5_2_min); // 跳转到日志页面. ie.ExecuteJQuery(JQuery.Create("'a:contains(日志)' ").Attr("'id'", "'rz'")); ie.ExecuteScript("document.getElementById('rz').click();"); ie.IEFlow.Wait(5); // 安装 jquery 脚本. ie.InstallJQuery(Properties.Resources.jquery_1_5_2_min); // 跳转到编辑日志页面. ie.ExecuteJQuery(JQuery.Create("'a:contains(写日志)' ").Attr("'id'", "'xrz'")); ie.ExecuteScript("document.getElementById('xrz').click();"); ie.IEFlow.Wait(5); // 安装 jquery 脚本. ie.InstallJQuery(Properties.Resources.jquery_1_5_2_min); // 填写日志内容. ie.ExecuteJQuery(JQuery.Create("'.ztag:text'").Val("'<标题>'")); ie.ExecuteJQuery(JQuery.Create("'#ne-auto-id-source'").Trigger("'click'")); ie.ExecuteJQuery(JQuery.Create("'textarea.ztag'").Val(string.Format("'{0}'", IEBrowser.EscapeCharacter("<日志 html 代码>")))); ie.ExecuteJQuery(JQuery.Create("'#ne-auto-id-source'").Trigger("'click'")); ie.ExecuteScript("document.getElementById('key-093402170-autotag').click();"); ie.IEFlow.Wait(5); // 发布日志. ie.ExecuteJQuery(JQuery.Create("'.fc09:button'").Attr("'id'", "'fb'")); ie.ExecuteScript("document.getElementById('fb').click();"); }