1. 程式人生 > >scrapy xpath解析出現:AttributeError: 'list' object has no attribute 'xpath'

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

當我們在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/

更多資源請關注公眾號(蛇崽網盤教程資源 ):

微信公眾號:蛇崽網盤教程資源

--------------------------------------- 上面是個人資訊 ------------------------------------------------