惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - 邢少

IM客户端Socks 5代理协议应用 招聘程序员的方法 CAS 与.net 集成的 “循环重定向”问题分析 .net 开发 跬步篇(6〕—绝对路径转换相对路径的 麻雀虽小,五脏俱全-C# 创建windows服务、socket通讯实例 asp.net 跬步篇(5) repeater 自定义模板实现特殊样式控件 asp.net 跬步篇(4) EnableSessionState设置 引起的框架集加载问题 控制CPU曲线引发的感想 asp.net 开发 跬步篇〔3〕.net 邮件批量发送 驱驾ViewState利剑—压缩ViewState 如何驾驭ViewState利剑 编程“方便面”之用户控件 .NET程序员应该知道些什么[转载] asp.net 开发 跬步篇(2) JQuery +ashx 升级之 JSon asp.net 开发 跬步篇〔1〕_ajax web页面复杂处理延时、客户交互问题 Asp .net +jquery +.ashx 文件实现分页 Gridview repeater datelist 区别 [技术思考]一段时间后的回首。 粮食的存在
天气数据一把抓。
邢少 · 2009-10-27 · via 博客园 - 邢少

   今天一位兄弟问了一个关于数据抓取的问题,刚好以前写过一个简单的天气预报的数据抓取功能。毕竟找一个免费的天气服务真实太不容易了,又不想花钱买,抓取吧!方便、简单!

       数据抓取主要是涉及到两方面的知识:

  1、正则就不说了,用多了也就熟练了。

  2、模拟登录 似很难,其实很简单,当然是只抓取一些公共的信息,如果有登录验证,也就不简单了。我今天也是说说简单的数据抓取功能,专取天气预报的数据。

  直接贴出代码吧,因为真是没什么可讲的,就是个知道与不知道的问题。告诉你,你就知道了。

/// <summary>
    
/// 得到天气数据
    
/// </summary>
    
/// <returns>数组(0、天气;1、气温;2、风力;3、紫外线;4、空气)</returns>

    public static string[] GetWeather()
    
{
        Regex regex;
       
        
string[] weather = new string[5];
        
string content = "";
        
        Match mcTmp;
        Match mcCity;
        
int k = 1;

        HttpWebResponse theResponse;
        WebRequest theRequest;


        theRequest 
= WebRequest.Create("http://weather.news.qq.com/inc/ss82.htm");
        
try
        
{
            theResponse 
= (HttpWebResponse)theRequest.GetResponse();

            
using (System.IO.Stream sm = theResponse.GetResponseStream())
            
{
                System.IO.StreamReader read 
= new System.IO.StreamReader(sm, Encoding.Default);
                content 
= read.ReadToEnd();
            }

        }

        
catch (Exception)
        
{
            content 
= "";
        }

     

        
string parttenTmp = "<td height=\"23\" width=\"117\" background=\"/images/r_tembg5.gif\" align=\"center\">(?<item1>[^<]+)</td>";
        k 
= 1;
        regex 
= new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        
for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
        
{

            weather[
0= mcTmp.Groups["item1"].Value;
        }

        parttenTmp 
= "height=\"23\" align=\"center\">(?<item1>[^/]+)</td>";
        k 
= 1;
        regex 
= new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        
for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
        
{
            weather[k] 
= mcTmp.Groups["item1"].Value;
        }

        
return weather;
    }

  看过上面的代码,应该明白了一些吧。只是路在那的问题,谁不会走路呢?这些只是简单的应用,数据抓取其实在很多的行业都会用到,举个简单的例子:搜索。搜索就是数据抓取的极端,当然数据抓取只是爬虫机器人的一小部分技术。就说到这把,下班喽!