首页 新闻 会员 周边

网页抓取奇怪问题

0
悬赏园豆:80 [待解决问题] 解决于 2011-06-30 21:03

http://www.tripadvisor.com/Hotels-g60763-New_York_City_New_York-Hotels.html

 我抓取酒店列表数据

如:Casablanca Hotel Times Square 

 

string strContent = client.GetProxyServerContent(url).Replace (@"
","");

 

 

private string hotelAllInfo = "<div\\s*class=\"listing(?<hotelAllInfo>.*?)<div";

Match match
= Regex.Match(strContent, hotelAllInfo, RegexOptions.IgnoreCase);
ArrayList alist
= new ArrayList();
while (match.Success)//列表首页抓取
{
....
}

页面内容能抓取到,我第一次正则表达式匹配内容都没问题的,都能提取到,但今天不行了,主要是匹配提取不到内容,只要是页面内容换行的,就匹配不到,而在一行时能匹配提取到
<div class="listing first" id="hotel_113317">
<div class="attnBar">
<div class="inner">
<span class="tc"><b>Travelers' Choice&#174; 2011 Winner</b>
<a class="js_popup" onclick="setPID(4045)" rel=nofollow href=/TravelersChoice-g191-cBestService-United_States.html>Best Service</a> | <a class="js_popup" onclick="setPID(4045)" rel=nofollow href=/TravelersChoice-g191-cTop25-United_States.html>Top 25</a>
</span>
<span class="tv"><b>Top Value!</b> Save vs. similar hotels in New York City</span>
</div>
当我    
private string hotelAllInfo = "<div\\s*class=\"listing(?<hotelAllInfo>.*?)>";
就能匹配提取到
first" id="hotel_113317"

第一次是 strContent 像上面那样做了一下换行代替,才可以的,现在这样不行了,不知道是我什么地方弄不同了是不是,NND ,搞了一天

那位牛人能指点一下啊

tiantiantian的主页 tiantiantian | 初学一级 | 园豆:0
提问于:2011-05-31 21:03
< >
分享
所有回答(5)
0

Match match = Regex.Match(strContent, hotelAllInfo, RegexOptions.Multiline | RegexOptions.IgnoreCase);
换为这样试试

artwl | 园豆:16736 (专家六级) | 2011-05-31 23:16
没有用,抓取的这个有换行的就匹配不到了
一行的内容可以匹配到
支持(0) 反对(0) tiantiantian | 园豆:0 (初学一级) | 2011-06-01 09:49
0

string strContent = client.GetProxyServerContent(url).Replace (@"
","").Replace("\r\n","");

Univ | 园豆:205 (菜鸟二级) | 2011-06-01 11:15
0

的确有 \r\的问题,之前研究过一段时间抓取数据,用正则截取是个办法,不过用分析dom更好一些,有开源的项目可以利用。

niugm | 园豆:1685 (小虾三级) | 2011-06-01 11:37
0

\s可以匹配不可见字符(包括换行)

duxer | 园豆:8 (初学一级) | 2011-06-03 23:52
0

使用.来匹配带有换行的所有字符时,需设置单行匹配RegexOptions.Singeline

Shiaupo | 园豆:272 (菜鸟二级) | 2011-08-26 15:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册