python爬虫

这里使用的是xpath这个第三方python库

使用 from lxml import etree 导入

 

使用:

select = etree.HTML(html)

content = select.xpath()

for each in content:

  print each

 

 

一个简单的demo:

from lxml import etree


html = '''
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <input id='name1' href = 'www.hao123.com'></input>
    <input id='name2'></input>
        xxxx


</body>
</html>
'''
select = etree.HTML(html)

content = select.xpath("//body/input[@id='name1']/@href")

for each in content:
    print each
print "end"

 

posted @ 2016-02-25 23:43  廖先生  阅读(421)  评论(0编辑  收藏  举报