首页 > 编程语言 > 详细

[Python 练习爬虫] XPATH基础语法

时间:2018-01-16 14:42:55      阅读:213      评论:0      收藏:0      [点我收藏+]

 

 

XPATH语法:

// 定位根标签

/ 往下层寻找

/text() 提取文本内容

/@xxx 提取属性内容

 

Sample:

import requests
from lxml import etree

for i in range(1, 21):
    url = "http://www.xxx.com/topic/tv/page/{}".format(i)
    req = requests.get(url).content

    html = etree.HTML(req)

    # 提取文本
    text = html.xpath(
        ‘/html/body/section/div[1]/div/article[*]/header/h2/a/text()‘)
    for each in text:
        print(1,each)

    # 提取链接
    # link = html.xpath(‘//a/@href‘)
    # for i in link:
    #     print(i)

  

 

[Python 练习爬虫] XPATH基础语法

原文:https://www.cnblogs.com/i-honey/p/8296039.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!