python + lxml 抓取网页 ,不需用正则,用xpath

我的第一个python入门程序:
python + lxml 抓取网页 ,不需用正则,用xpath


# -*- coding:gb2312 -*-
import urllib
import hashlib
import os
class Spider:
'''crawler html'''
def get_html(self,url):
sock = urllib.urlopen(url)
htmlSource = sock.read()
sock.close()
return htmlSource
def cache_html(self,filename,htmlSource):
f = open(filename,'w')
f.write(htmlSource)
f.close
def analysis_html(self,htmlSource):
#from lxml import etree
import lxml.html.soupparser as soupparser
dom = soupparser.fromstring(htmlSource)
#doc = dom.parse(dom)
r = dom.xpath(".//*[@id='lh']/a[2]")
print len(r)
print r[0].tag
'''
这里直接输出中文print r[0].text 会报错,所以用了encode('gb2312')
并且在文件头部声明了文件编码类型
参考:http://blogold.chinaunix.net/u2/60332/showart_2109290.html
'''
print r[0].text.encode('gb2312')
print 'done'
def get_cache_html(self,filename):
if not os.path.isfile(filename):
return ''
f = open(filename,'r')
content = f.read()
f.close()
return content
if __name__ == '__main__':
spider = Spider()
url = 'http://www.baidu.com'
md5_str = hashlib.md5(url).hexdigest()
filename = "html-"+md5_str+".html"
htmlSource = spider.get_cache_html(filename);
if not htmlSource:
htmlSource = spider.get_html(url)
spider.cache_html(filename,htmlSource)
spider.analysis_html(htmlSource)



程序流程:
抓取页面:get_html
保存页面:cache_html
分析页面:analysis_html

辅助方法:get_cache_html,如果已经抓取过的页面,保存为本地文件,下一次直接从本地文件取html内容,不用再次通过网络抓取

xpath分析工具:firefox插件,firepath

[img]http://dl.iteye.com/upload/attachment/553501/98d34f25-76fa-3319-a1c9-15e1a4e341cd.jpg[/img]

lxml 学习参考:http://lxml.de/index.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值