爬虫中网页分析的几种技术

一般来说我们只抓取网页中的特定数据,比如抓取某人所有的blog,我们就只关心list 页面中文章列表那部分的链接和title


有几种技术可以用来分析网页

1)正则匹配

2)一般字符串匹配content.substring(pattern, startIndex),一般是带有startIndex的substring,而不是每次都是从头匹配

3) 基于sax的事件

4)DOM + XPath


抓去的数据有两种

1)基于数据本身的parttern,比如链接、email adrress,适合用正则

2)基于位置。数据本身没什么特别,关键在于在什么位置出现。适合用其他3种,


基于sax事件的最好,流式处理,不需要存储整个网页,缺点是有些网页不规范,sax 要求必须是合法、well formed xml。

substring和正则一般需要先把网页读成字符串,substring更简单轻量级一些,

DOM+xpath太杀鸡用牛刀了


例子,把自己csdn上所有的博文扒下来:

from urllib2 import Request, urlopen, URLError

page, articleList, visited, startOver = 1, [], set(), False
while not startOver:
    req = Request('http://blog.csdn.net/binling/article/list/' + str(page), headers={'User-agent': 'Mozilla 5.10'})
    try:content = urlopen(req).read()
    except URLError, e: break
    pos = 0
    while True:
        try:
            pos = content.index('link_title', pos)
            pos = content.index('href', pos)
            pos = content.index('"', pos)
            end = content.index('"', pos + 1)
            link = content[pos + 1:end].strip().decode('utf-8')
            if link in visited:
                startOver = True
                break
            pos = content.index('>', end)
            end = content.index('</a>', pos)
            title = content[pos + 1: end].strip()
            articleList.append((title.decode('utf-8'), link))
            visited.add(link)
        except: break
    page += 1

home = 'C:\\Personal\\CSDN'
for title, link in articleList:
    for c in '/\*:<>?"|':
        if c in title: title = title.replace(c, ' ')
    content = urlopen(Request('http://blog.csdn.net' + link, headers={'User-agent': 'Mozilla 5.10'})).read()
    with open(home + '\\' + title + '.html', 'w') as f:
        f.write(content)
        print title









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值