mshtml简单代码获得Google Adsense广告路径[代码]
最近分析了一下Google Adsense投放的广告,呵呵,使用mshtml很简单的代码得到Google Adsense的广告的地址,没什么用处,呵呵,只是玩玩而已,声明不会对Google Adsense进行作弊,不然会被Google给kick out的,呵呵……
至于原理很简单:Google利用Javascript在目的网页上生成一个name为google_ads_frame的iframe,该iframe中的网页上包含一个id为aw0的超级链接(代码中分析的是按钮型的广告,其中只有一个超级链接,多个超级的原理也是一样的,超级链接的id的格式为“aw+序数”),这些用mshtml都很容易得到。
需要提到的是广告中的超级链接的类型(用mshtml分析)为HTMLAnchorElementClass。如果不知道类型怎么办,可以用this.doc.all.item("aw0", null).GetType().ToString()来得到,这是使用mshtml时的一个技巧。
代码:
1using System;
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6
7namespace Google_Adsense
8{
9 /// <summary>
10 /// frmGetAdLink 的摘要说明。
11 /// </summary>
12 public class frmGetAdLink : System.Windows.Forms.Form
13 {
14 private System.Windows.Forms.Timer timRefresh;
15 private System.Windows.Forms.Panel panBottom;
16 private System.Windows.Forms.Button btnLink;
17 private System.Windows.Forms.Button btnAds;
18 private System.Windows.Forms.Button btnExit;
19 private AxSHDocVw.AxWebBrowser awbMain;
20 private System.ComponentModel.IContainer components;
21
22 public frmGetAdLink()
23 {
24 //
25 // Windows 窗体设计器支持所必需的
26 //
27 InitializeComponent();
28
29 //
30 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
31 //
32 }
33
34 /// <summary>
35 /// 清理所有正在使用的资源。
36 /// </summary>
37 protected override void Dispose( bool disposing )
38 {
39 if( disposing )
40 {
41 if(components != null)
42 {
43 components.Dispose();
44 }
45 }
46 base.Dispose( disposing );
47 }
48
49 #region Windows 窗体设计器生成的代码
50 /// <summary>
51 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
52 /// 此方法的内容。
53 /// </summary>
54 private void InitializeComponent()
55 {
56 this.components = new System.ComponentModel.Container();
57 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmGetAdLink));
58 this.timRefresh = new System.Windows.Forms.Timer(this.components);
59 this.panBottom = new System.Windows.Forms.Panel();
60 this.btnLink = new System.Windows.Forms.Button();
61 this.btnAds = new System.Windows.Forms.Button();
62 this.btnExit = new System.Windows.Forms.Button();
63 this.awbMain = new AxSHDocVw.AxWebBrowser();
64 this.panBottom.SuspendLayout();
65 ((System.ComponentModel.ISupportInitialize)(this.awbMain)).BeginInit();
66 this.SuspendLayout();
67 //
68 // timRefresh
69 //
70 this.timRefresh.Interval = 20000;
71 //
72 // panBottom
73 //
74 this.panBottom.Controls.Add(this.btnLink);
75 this.panBottom.Controls.Add(this.btnAds);
76 this.panBottom.Controls.Add(this.btnExit);
77 this.panBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
78 this.panBottom.Location = new System.Drawing.Point(0, 328);
79 this.panBottom.Name = "panBottom";
80 this.panBottom.Size = new System.Drawing.Size(626, 56);
81 this.panBottom.TabIndex = 1;
82 //
83 // btnLink
84 //
85 this.btnLink.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
86 this.btnLink.Enabled = false;
87 this.btnLink.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
88 this.btnLink.Location = new System.Drawing.Point(450, 16);
89 this.btnLink.Name = "btnLink";
90 this.btnLink.TabIndex = 9;
91 this.btnLink.Text = "Link(&L)";
92 this.btnLink.Click += new System.EventHandler(this.btnLink_Click);
93 //
94 // btnAds
95 //
96 this.btnAds.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
97 this.btnAds.Enabled = false;
98 this.btnAds.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
99 this.btnAds.Location = new System.Drawing.Point(362, 16);
100 this.btnAds.Name = "btnAds";
101 this.btnAds.TabIndex = 8;
102 this.btnAds.Text = "Ads(&A)";
103 this.btnAds.Click += new System.EventHandler(this.btnAds_Click);
104 //
105 // btnExit
106 //
107 this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
108 this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
109 this.btnExit.Location = new System.Drawing.Point(538, 16);
110 this.btnExit.Name = "btnExit";
111 this.btnExit.TabIndex = 4;
112 this.btnExit.Text = "退出(&X)";
113 this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
114 //
115 // awbMain
116 //
117 this.awbMain.Dock = System.Windows.Forms.DockStyle.Fill;
118 this.awbMain.Enabled = true;
119 this.awbMain.Location = new System.Drawing.Point(0, 0);
120 this.awbMain.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("awbMain.OcxState")));
121 this.awbMain.Size = new System.Drawing.Size(626, 328);
122 this.awbMain.TabIndex = 2;
123 this.awbMain.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.awbMain_DocumentComplete);
124 //
125 // frmGetAdLink
126 //
127 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
128 this.BackColor = System.Drawing.SystemColors.Control;
129 this.ClientSize = new System.Drawing.Size(626, 384);
130 this.Controls.Add(this.awbMain);
131 this.Controls.Add(this.panBottom);
132 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
133 this.Name = "frmGetAdLink";
134 this.Text = "mshtml获得广告Google Adsense路径";
135 this.Load += new System.EventHandler(this.frmGetAdLink_Load);
136 this.panBottom.ResumeLayout(false);
137 ((System.ComponentModel.ISupportInitialize)(this.awbMain)).EndInit();
138 this.ResumeLayout(false);
139
140 }
141 #endregion
142
143 /// <summary>
144 /// 应用程序的主入口点。
145 /// </summary>
146 [STAThread]
147 static void Main()
148 {
149 Application.Run(new frmGetAdLink());
150 }
151
152
153 private mshtml.IHTMLDocument2 doc = null;
154
155 private mshtml.IHTMLWindow2 win = null;
156
157 private int intDCCount = 0;
158
159 private void frmGetAdLink_Load(object sender, System.EventArgs e)
160 {
161 object url = null;
162
163 url = "http://www.cnblogs.com/waxdoll";
164
165 object r = System.Reflection.Missing.Value;
166
167 this.awbMain.Navigate2(ref url,ref r,ref r,ref r,ref r);
168 }
169
170
171 private void awbMain_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
172 {
173 this.doc = (mshtml.IHTMLDocument2)this.awbMain.Document;
174
175 this.win = this.doc.parentWindow;
176
177 if (this.intDCCount == 0)
178 this.btnAds.Enabled = true;
179
180 this.intDCCount ++;
181 }
182
183 /// <summary>
184 /// 在浏览器中显示Google Adsense的广告
185 /// </summary>
186 /// <param name="sender"></param>
187 /// <param name="e"></param>
188 private void btnAds_Click(object sender, System.EventArgs e)
189 {
190
191 this.btnAds.Enabled = false;
192
193 mshtml.IHTMLIFrameElement eleIframe = (mshtml.IHTMLIFrameElement)this.doc.all.item("google_ads_frame", null);
194
195 object url = null;
196
197 url = ((mshtml.HTMLIFrameClass)eleIframe).src;
198
199 object r = System.Reflection.Missing.Value;
200
201 this.awbMain.Navigate2(ref url, ref r, ref r, ref r, ref r);
202
203 this.btnLink.Enabled = true;
204
205 eleIframe = null;
206 }
207
208 /// <summary>
209 /// 弹出对话框显示广告中的地址
210 /// </summary>
211 /// <param name="sender"></param>
212 /// <param name="e"></param>
213 private void btnLink_Click(object sender, System.EventArgs e)
214 {
215 mshtml.IHTMLElement2 eleLink = (mshtml.IHTMLElement2)this.doc.all.item("aw0", null);
216
217
218 System.Windows.Forms.MessageBox.Show(((mshtml.HTMLAnchorElementClass)eleLink).href);
219 }
220
221
222 private void btnExit_Click(object sender, System.EventArgs e)
223 {
224 this.Dispose();
225 }
226
227 }
228}
229
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6
7namespace Google_Adsense
8{
9 /// <summary>
10 /// frmGetAdLink 的摘要说明。
11 /// </summary>
12 public class frmGetAdLink : System.Windows.Forms.Form
13 {
14 private System.Windows.Forms.Timer timRefresh;
15 private System.Windows.Forms.Panel panBottom;
16 private System.Windows.Forms.Button btnLink;
17 private System.Windows.Forms.Button btnAds;
18 private System.Windows.Forms.Button btnExit;
19 private AxSHDocVw.AxWebBrowser awbMain;
20 private System.ComponentModel.IContainer components;
21
22 public frmGetAdLink()
23 {
24 //
25 // Windows 窗体设计器支持所必需的
26 //
27 InitializeComponent();
28
29 //
30 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
31 //
32 }
33
34 /// <summary>
35 /// 清理所有正在使用的资源。
36 /// </summary>
37 protected override void Dispose( bool disposing )
38 {
39 if( disposing )
40 {
41 if(components != null)
42 {
43 components.Dispose();
44 }
45 }
46 base.Dispose( disposing );
47 }
48
49 #region Windows 窗体设计器生成的代码
50 /// <summary>
51 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
52 /// 此方法的内容。
53 /// </summary>
54 private void InitializeComponent()
55 {
56 this.components = new System.ComponentModel.Container();
57 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmGetAdLink));
58 this.timRefresh = new System.Windows.Forms.Timer(this.components);
59 this.panBottom = new System.Windows.Forms.Panel();
60 this.btnLink = new System.Windows.Forms.Button();
61 this.btnAds = new System.Windows.Forms.Button();
62 this.btnExit = new System.Windows.Forms.Button();
63 this.awbMain = new AxSHDocVw.AxWebBrowser();
64 this.panBottom.SuspendLayout();
65 ((System.ComponentModel.ISupportInitialize)(this.awbMain)).BeginInit();
66 this.SuspendLayout();
67 //
68 // timRefresh
69 //
70 this.timRefresh.Interval = 20000;
71 //
72 // panBottom
73 //
74 this.panBottom.Controls.Add(this.btnLink);
75 this.panBottom.Controls.Add(this.btnAds);
76 this.panBottom.Controls.Add(this.btnExit);
77 this.panBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
78 this.panBottom.Location = new System.Drawing.Point(0, 328);
79 this.panBottom.Name = "panBottom";
80 this.panBottom.Size = new System.Drawing.Size(626, 56);
81 this.panBottom.TabIndex = 1;
82 //
83 // btnLink
84 //
85 this.btnLink.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
86 this.btnLink.Enabled = false;
87 this.btnLink.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
88 this.btnLink.Location = new System.Drawing.Point(450, 16);
89 this.btnLink.Name = "btnLink";
90 this.btnLink.TabIndex = 9;
91 this.btnLink.Text = "Link(&L)";
92 this.btnLink.Click += new System.EventHandler(this.btnLink_Click);
93 //
94 // btnAds
95 //
96 this.btnAds.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
97 this.btnAds.Enabled = false;
98 this.btnAds.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
99 this.btnAds.Location = new System.Drawing.Point(362, 16);
100 this.btnAds.Name = "btnAds";
101 this.btnAds.TabIndex = 8;
102 this.btnAds.Text = "Ads(&A)";
103 this.btnAds.Click += new System.EventHandler(this.btnAds_Click);
104 //
105 // btnExit
106 //
107 this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
108 this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
109 this.btnExit.Location = new System.Drawing.Point(538, 16);
110 this.btnExit.Name = "btnExit";
111 this.btnExit.TabIndex = 4;
112 this.btnExit.Text = "退出(&X)";
113 this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
114 //
115 // awbMain
116 //
117 this.awbMain.Dock = System.Windows.Forms.DockStyle.Fill;
118 this.awbMain.Enabled = true;
119 this.awbMain.Location = new System.Drawing.Point(0, 0);
120 this.awbMain.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("awbMain.OcxState")));
121 this.awbMain.Size = new System.Drawing.Size(626, 328);
122 this.awbMain.TabIndex = 2;
123 this.awbMain.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.awbMain_DocumentComplete);
124 //
125 // frmGetAdLink
126 //
127 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
128 this.BackColor = System.Drawing.SystemColors.Control;
129 this.ClientSize = new System.Drawing.Size(626, 384);
130 this.Controls.Add(this.awbMain);
131 this.Controls.Add(this.panBottom);
132 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
133 this.Name = "frmGetAdLink";
134 this.Text = "mshtml获得广告Google Adsense路径";
135 this.Load += new System.EventHandler(this.frmGetAdLink_Load);
136 this.panBottom.ResumeLayout(false);
137 ((System.ComponentModel.ISupportInitialize)(this.awbMain)).EndInit();
138 this.ResumeLayout(false);
139
140 }
141 #endregion
142
143 /// <summary>
144 /// 应用程序的主入口点。
145 /// </summary>
146 [STAThread]
147 static void Main()
148 {
149 Application.Run(new frmGetAdLink());
150 }
151
152
153 private mshtml.IHTMLDocument2 doc = null;
154
155 private mshtml.IHTMLWindow2 win = null;
156
157 private int intDCCount = 0;
158
159 private void frmGetAdLink_Load(object sender, System.EventArgs e)
160 {
161 object url = null;
162
163 url = "http://www.cnblogs.com/waxdoll";
164
165 object r = System.Reflection.Missing.Value;
166
167 this.awbMain.Navigate2(ref url,ref r,ref r,ref r,ref r);
168 }
169
170
171 private void awbMain_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
172 {
173 this.doc = (mshtml.IHTMLDocument2)this.awbMain.Document;
174
175 this.win = this.doc.parentWindow;
176
177 if (this.intDCCount == 0)
178 this.btnAds.Enabled = true;
179
180 this.intDCCount ++;
181 }
182
183 /// <summary>
184 /// 在浏览器中显示Google Adsense的广告
185 /// </summary>
186 /// <param name="sender"></param>
187 /// <param name="e"></param>
188 private void btnAds_Click(object sender, System.EventArgs e)
189 {
190
191 this.btnAds.Enabled = false;
192
193 mshtml.IHTMLIFrameElement eleIframe = (mshtml.IHTMLIFrameElement)this.doc.all.item("google_ads_frame", null);
194
195 object url = null;
196
197 url = ((mshtml.HTMLIFrameClass)eleIframe).src;
198
199 object r = System.Reflection.Missing.Value;
200
201 this.awbMain.Navigate2(ref url, ref r, ref r, ref r, ref r);
202
203 this.btnLink.Enabled = true;
204
205 eleIframe = null;
206 }
207
208 /// <summary>
209 /// 弹出对话框显示广告中的地址
210 /// </summary>
211 /// <param name="sender"></param>
212 /// <param name="e"></param>
213 private void btnLink_Click(object sender, System.EventArgs e)
214 {
215 mshtml.IHTMLElement2 eleLink = (mshtml.IHTMLElement2)this.doc.all.item("aw0", null);
216
217
218 System.Windows.Forms.MessageBox.Show(((mshtml.HTMLAnchorElementClass)eleLink).href);
219 }
220
221
222 private void btnExit_Click(object sender, System.EventArgs e)
223 {
224 this.Dispose();
225 }
226
227 }
228}
229
Life is like a boat, and I'm at sea.