1. 程式人生 > 實用技巧 >記錄犯得最可笑的錯誤

記錄犯得最可笑的錯誤

程式碼引用

        detail_lists = response.xpath('//section[@class="section board-list board-list-collapse"]/ul/li')
        #'//section[@class="section board-list board-list-collapse"]/ul/li'
        print('-----> ????????????????????????')
        print(detail_lists)
        for detail in detail_lists:
            novelimg = detail.xpath('./a/img/@src').extract_first()
            # '//*[@id="navList"]/section/ul/li[32]/a/img'
            novelname = detail.xpath('./span/a[1]/@title').extract_first()
            # '//*[@id="navList"]/section/ul/li[32]/span/a[1]'
            href = detail.xpath('./a/@href').extract_first()
            # '//*[@id="navList"]/section/ul/li[32]/a'
            author = detail.xpath('./span/a[2]/text()').extract_first()
            # '//*[@id="navList"]/section/ul/li[32]/span/a[2]'
            state = detail.xpath('./img[@class="topss png_bg"]/@src').extract_first()
            # '//*[@id="navList"]/section/ul/li[32]/img'
            print('圖片:', novelimg)
            print('小說名稱:', novelname)
            print('連結:', href)
            print('作者是:', author)
            if 'only2.png' in state:
                state = '連載中'
            else:
                state = '完結'
            print('狀態是:', state)

一直解析不出標籤內容和屬性值的原因

      for detail in detail_lists:
            novelimg = response.xpath('./a/img/@src').extract_first()
      # 將for語句中的xpath解析寫成了response.xpath,應該是detail.xpath
      # 寫程式碼邏輯錯誤,跳過錯誤,不是一行行寫的,跳躍性敲程式碼,拼湊的程式碼,很噁心