Scrapy - Extract items from table

后端 未结 3 1017
天命终不由人
天命终不由人 2021-01-02 09:42

Trying to get my head around Scrapy but hitting a few dead ends.

I have a 2 Tables on a page and would like to extract the data from each one then move along to the

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-02 10:29

    You need to slightly correct your code. Since you already select all elements within the table you don't need to point again to a table. Thus you can shorten your xpath to something like thistd[1]//text().

    def parse_products(self, response):
        products = response.xpath('//*[@id="Year1"]/table//tr')
        # ignore the table header row
        for product in products[1:]  
           item = Schooldates1Item()
           item['hol'] = product.xpath('td[1]//text()').extract_first()
           item['first'] = product.xpath('td[2]//text()').extract_first()
           item['last'] = product.xpath('td[3]//text()').extract_first()
           yield item
    

    Edited my answer since @stutray provide the link to a site.

提交回复
热议问题