Python Scrapy-将表格标题(thead)值添加到项目加载器

Python Scrapy-将表格标题(thead)值添加到项目加载器,python,scrapy,Python,Scrapy,我有一个网页,其中包含我希望使用Scrapy刮取的多个表: <table> <thead> <tr> <th> <a>Heading1</a> </th> </tr> <tr> <th>Col1</th> <th>Co

我有一个网页,其中包含我希望使用Scrapy刮取的多个表:

<table>
   <thead>
      <tr>
         <th>
            <a>Heading1</a>
         </th>
      </tr>
      <tr>
         <th>Col1</th>
         <th>Col2</th>
         <th>Col3</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td><a href="#">Name1</a></td>
         <td>Description1</td>
         <td>Number1</td>
      </tr>
      <tr>
         <td><a href="#">Name2</a></td>
         <td>Description2</td>
         <td>Number2</td>
      </tr>

      ...

    </tbody>
</table>
这非常有效,我可以在页面上同一个表的所有实例上用每行数据填充我的项目加载器

不过,我的问题是:

如何向我的项目加载器添加第四个字段,该字段包含我刮取的每个表的“标题”文本?

提前感谢您的帮助


编辑

这是我目前可以收集的数据样本:

Name1 | Description1 | Number1
Name2 | Description2 | Number2
...

# and so forth for the other table instances:

Name3 | Description3 | Number3
Name4 | Description4 | Number4
...
这就是我想要的:

Name1 | Description1 | Number1 | Heading1
Name2 | Description2 | Number2 | Heading1
...

# and so forth for the other table instances:

Name3 | Description3 | Number3 | Heading2
Name4 | Description4 | Number4 | Heading2
...

我希望我理解正确,可能是这样的:

def parse(self, response):
   hxs = HtmlXPathSelector(response)
   for tb in hxs.xpath('//table'):

       heading = tb.xpath('.//thead/tr/th/a/text()').extract()[0]

       for td in tb.xpath('.//tbody/tr'):
          il = WebsiteLoader(response=response, selector=td)
          ...
          il.add_value('heading', heading)
          yield il.load_item()

有人能帮我解决这个问题吗?很难理解你想要实现什么,标题是每个表的,你要求为你的项目设置第四个字段?是否要将所有项目中的标题作为第四个字段重复?如果是的话,你会怎么说。。。项目的单个字段中有4个标题?逗号分隔…@谢谢你的回复。每个表都没有单独的标题,我正在将数据刮到数据库中。每行我都想在它的记录中添加标题的第四个字段。因此,是的,我想重复标题。请添加一个完整项目的示例,包括第四个字段。因此,鉴于您共享的表格html,标题是什么<代码>标题1,第1列,第2列,第3列在该表的每一行(项目)重复?太好了,现在我分配了第一个标题,
标题1
,谢谢!但是,下一个表的值不会更改为
Heading2
,而是使用相同的
Heading1
值。这就是我目前被困的地方。非常感谢!我确实有一个索引超出范围的错误,但我认为有一个幻影表被拾取,所以我将其更改为我想要的table类,它工作得很好!我猜您的一个/一些表缺少这种结构,如果看不到真正的html页面就很难说,请尝试打印
tb.xpath('.//thead/').extract()
,然后查看我编辑的注释-还有一个表与其他表不一样-非常感谢您的帮助:)
def parse(self, response):
   hxs = HtmlXPathSelector(response)
   for tb in hxs.xpath('//table'):

       heading = tb.xpath('.//thead/tr/th/a/text()').extract()[0]

       for td in tb.xpath('.//tbody/tr'):
          il = WebsiteLoader(response=response, selector=td)
          ...
          il.add_value('heading', heading)
          yield il.load_item()