<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>白舟空间 &#187; aspx</title>
	<atom:link href="http://www.baizoo.cn/tag/aspx/feed" rel="self" type="application/rss+xml" />
	<link>http://www.baizoo.cn</link>
	<description>乘风破浪会有时</description>
	<lastBuildDate>Mon, 26 Jul 2010 15:58:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>aspx采集alexa方法整理</title>
		<link>http://www.baizoo.cn/archives/919.html</link>
		<comments>http://www.baizoo.cn/archives/919.html#comments</comments>
		<pubDate>Fri, 22 Jan 2010 20:53:48 +0000</pubDate>
		<dc:creator>baizoo</dc:creator>
				<category><![CDATA[技术爱好]]></category>
		<category><![CDATA[aspx]]></category>
		<category><![CDATA[采集]]></category>

		<guid isPermaLink="false">http://www.baizoo.cn/?p=919</guid>
		<description><![CDATA[刚学了php采集的技巧，正好手头有个aspx相关的源码，就顺便学习一下。 首先提个aspx的小技巧，vs里面默认的aspx页面是设计和编程分开的，两者分别在.aspx和.cs页面，而它们关联的前提是在.aspx页面里添加了下面代码： &#60;%@ Page Language=&#34;C#&#34; AutoEventWireup=&#34;true&#34; CodeBehind=&#34;Default.aspx.cs&#34; Inherits=&#34;web._Default&#34; %&#62; 如果要把代码直接写到aspx页面里，则头部代码改为 &#60;%@ Page Language=&#34;C#&#34; AutoEventWireup=&#34;true&#34; %&#62;&#60;%@ Import Namespace=&#34;System.IO&#34; %&#62;&#60;%@ Import Namespace=&#34;System.Net&#34; %&#62; 言归正传，下面整理aspx采集alexa值的方法，这里有个获取页面源码的小函数GetHtml public HttpWebRequest request;&#160;&#160; &#160;public HttpWebResponse response;&#160;&#160; &#160;public string GetHtml(string url, string encode)&#160;&#160; &#160;{&#160;&#160; &#160; &#160; &#160;string str = null;&#160;&#160; &#160; &#160; &#160;request = (HttpWebRequest)WebRequest.Create(url);&#160;&#160; &#160; &#160; &#160;request.Method = &#34;Get&#34;;&#160;&#160; &#160; &#160; &#160;request.ContentType = [...]]]></description>
			<content:encoded><![CDATA[<p>刚学了php采集的技巧，正好手头有个aspx相关的源码，就顺便学习一下。</p>
<p>首先提个aspx的小技巧，vs里面默认的aspx页面是设计和编程分开的，两者分别在.aspx和.cs页面，而它们关联的前提是在.aspx页面里添加了下面代码：</p>
<div class="hl-surround"><div class="hl-main">&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Default.aspx.cs&quot; Inherits=&quot;web._Default&quot; %&gt;</div></div>
<p>如果要把代码直接写到aspx页面里，则头部代码改为</p>
<div class="hl-surround"><div class="hl-main">&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; %&gt;<br />&lt;%@ Import Namespace=&quot;System.IO&quot; %&gt;<br />&lt;%@ Import Namespace=&quot;System.Net&quot; %&gt;</div></div>
<p>言归正传，下面整理aspx采集alexa值的方法，这里有个获取页面源码的小函数GetHtml<br />
<span id="more-919"></span>
<div class="hl-surround"><div class="hl-main">public HttpWebRequest request;<br />&nbsp;&nbsp; &nbsp;public HttpWebResponse response;<br />&nbsp;&nbsp; &nbsp;public string GetHtml(string url, string encode)<br />&nbsp;&nbsp; &nbsp;{<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;string str = null;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;request = (HttpWebRequest)WebRequest.Create(url);<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;request.Method = &quot;Get&quot;;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;request.ContentType = &quot;application/x-www-form-urlencoded&quot;;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;response = (HttpWebResponse)request.GetResponse();<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if (response.StatusCode == HttpStatusCode.OK)<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StreamReader sr = null;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch (encode)<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &quot;UTF-8&quot;:<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &quot;GBK&quot;:<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(&quot;GBK&quot;));<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case &quot;GB2312&quot;:<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(&quot;GB2312&quot;));<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sr = new StreamReader(response.GetResponseStream(), Encoding.Default);<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;str = sr.ReadToEnd();<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return str;<br />&nbsp;&nbsp; &nbsp;}</div></div>
<p>调用这个函数</p>
<div class="hl-surround"><div class="hl-main">protected void Page_Load(object sender, EventArgs e)<br />&nbsp;&nbsp; &nbsp;{<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;string url= Request.QueryString[&quot;url&quot;];<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;string html=GetHtml(&quot;http://www.alexa.com/siteinfo/&quot;+url,&quot;UTF-8&quot;);&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;string regex = @&quot;style=&quot;&quot;margin-bottom:-2px;&quot;&quot;/&gt; (?&lt;alexa&gt;[^&lt;]*)&lt;/a&gt;&quot;;//用正则过滤得到需要的内容alexa值 <br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MatchCollection mc = Regex.Matches(html, regex, RegexOptions.IgnoreCase);//获取正则匹配的内容<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;string alexa = null;&nbsp; &nbsp; &nbsp; <br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;foreach (Match m in mc)<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alexa = m.Groups[&quot;alexa&quot;].Value.ToString();<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;divshow.InnerHtml = alexa;//把alexa值赋值给divshow<br />&nbsp;&nbsp; &nbsp;}</div></div>
<p>页面里加个显示控件</p>
<div class="hl-surround"><div class="hl-main">&lt;body&gt;<br />&nbsp;&nbsp; &nbsp;&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;<br />&nbsp;&nbsp; &nbsp;&lt;div runat=&quot;server&quot; id=&quot;divshow&quot;&gt;&nbsp; &nbsp; <br />&nbsp;&nbsp; &nbsp;&lt;/div&gt;<br />&nbsp;&nbsp; &nbsp;&lt;/form&gt;<br />&lt;/body&gt;<br /><br />最终效果浏览：http://localhost/alexa.aspx?url=baizoo.cn</div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.baizoo.cn/archives/919.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
