python静态网页爬虫之xpath

常用语句:

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

  

posted @ 2016-05-18 22:32  编程青年的崛起  阅读(436)  评论(0编辑  收藏  举报