scrapy xpath解析出现:AttributeError: 'list' object has no attribute 'xpath'

版权声明:本文为徐代龙原创文章,未经徐代龙允许不得转载。网络资源网站:xudailong.cc 福利网站:www.00reso.com 公众号:蛇崽网盘教程资源 https://blog.csdn.net/xudailong_blog/article/details/83545535

当我们在tbody标签里面取多个tr标签里面的内容时,我们一般都会取出个list集合,然后再进行遍历,获取里面的标签内容。

   node_list = response.xpath("//*[@class='list_1']/tr").extract()
        for node in node_list:
            paiming = node.xpath("./td[1]/text()").extract_first()
            if paiming is None:
                continue
            # print(paiming)
            link = node.xpath("./td[@class='team']/a/@href").extract_first()

说明一下:这里的node_list是通过extract()进行获取出来的,在遍历node_list会出现如AttributeError: 'list' object has no attribute 'xpath'的错误,此时,我们需要将上面的代码改为:

        node_list = response.xpath("//*[@class='list_1']/tr")
        for node in node_list:
            paiming = node.xpath("./td[1]/text()").extract_first()
            if paiming is None:
                continue
            # print(paiming)
            link = node.xpath("./td[@class='team']/a/@href").extract_first()

主要是去掉extract()这个,其实你可以打印一下node这个值的属性,你变晓得为什么需要这样子改了。

--------------------------------------- 下面是个人信息 ------------------------------------------------

个人微信:hll643435675(备注:博客)

更多资源请访问:

https://blog.csdn.net/xudailong_blog/article/details/78762262

慕课视频教程:https://blog.csdn.net/xudailong_blog/article/details/82909611

https://xudailong.cc/2018/09/30/muke-courses/

更多资源请关注公众号(蛇崽网盘教程资源 ):

微信公众号:蛇崽网盘教程资源

--------------------------------------- 上面是个人信息 ------------------------------------------------

猜你喜欢

转载自blog.csdn.net/xudailong_blog/article/details/83545535
今日推荐