如何从htm中获取表数据

2024-05-13 00:06:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个html中有多个表。以下图片示例:

enter image description here

我只是从表[6]——表[20]。在

编写以下xpath get表[6]。。表[20]不起作用。在

def parse_item(self, response):
        trs = response.xpath("/html/body/table[4]/tr/td[1]/table")
        for i in trs:
            item = CompanyItem()
            item['collection_dt'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
            item['company_hangye'] = '新能源(储能,太阳能,风能,光伏,新材料等)'
            item['company_quyue'] = i.xpath("/tr/td[1]/table[1]/tr/td[1]/text()").extract()
            item['company_name'] = i.xpath("/tr//td[1]/table[1]/tr/td[1]/strong/a/text()").extract_first()
            item['company_product'] = i.xpath("/tr//td[1]/table[4]/tr/td[1]/text()").extract()
            item['company_qiyeleixing'] = i.xpath("/tr//td[1]/table[5]/tr/td[2]/text()").extract()
            item['company_description'] = i.xpath("/tr//td[1]/table[2]/tr/td[1]/text()").extract()
            lianxi = i.xpath("/tr/td[1]/table[3]/tr/td[1]/text()").extract()
            item['company_lianxi_emp_name'] = lianxi[1]
            item['company_lianxi_tel'] = lianxi[2]
            item['company_lianxi_mobile'] = lianxi[3]
            return item

Tags: textnametimeresponsehtmltable图片extract