首页 > 技术文章 > python静态网页爬虫之xpath

alan-babyblog 2016-05-18 22:32 原文

常用语句:

1.starts-with(@属性名称,属性字符相同部分)使用情形: 以相同的字符开头

<div id = 'test-1'>需要的内容1</div>

<div id = 'test-2'>需要的内容2</div>

<div id = 'test-3'>需要的内容3</div>

selector = etree.HTML(html)
content = selector.xpath('//div[start-with(@id,'test')]/text()')

  

2.string(.) 使用情形:标签套标签

<div id='class3'>美女,

  <font color=red>你微信号是多少?</font>

</div>

selector = etree.HTML(html)
data = selector.xpath('//div[@id='test3']')[0]   #先大后小
info = data.xpath('string(.)')
content = info.replace('\n','').replace('  ','')  #替换换行符和tab

  

推荐阅读