Python 从表中提取(刮屑)

Python 从表中提取(刮屑),python,html,xpath,scrapy,Python,Html,Xpath,Scrapy,想在python2中使用scrapy请求表解析方面的帮助吗 这是我的桌子: 我需要获取标记的值。 尝试使用下一个python代码: rows = resp.xpath('//*[@id="Vorlage_Infobox_Unternehmen"]') if not rows: rows = resp.xpath('.//*[@id="Vorlage_Infobox_Unternehmen"]//table') try: if rows: extract = lam

想在python2中使用scrapy请求表解析方面的帮助吗 这是我的桌子: 我需要获取
标记的值。 尝试使用下一个python代码:

rows = resp.xpath('//*[@id="Vorlage_Infobox_Unternehmen"]')
if not rows:
    rows = resp.xpath('.//*[@id="Vorlage_Infobox_Unternehmen"]//table')
try:
    if rows:
        extract = lambda row, path: row.xpath(path).extract_first().strip()
        if '<th>' in str(rows):
            infobox = {extract(row, 'string(./th)'): extract(row, 'string(./td)') for row in rows}
        elif '<tr>' in str(rows):
            infobox = {extract(row, 'string(./td[1])'): extract(row, 'string(./td[2])') for row in rows}
        elif '<table>' in str(rows):
            infobox = {extract(row, 'string(./th)'): extract(row, 'string(./td)') for row in rows}
        else:
            infobox = {extract(row, 'string(./table/tbody/tr[1])'): extract(row, 'string(./td[1])') for row in rows}
rows=resp.xpath('/*[@id=“Vorlage\u Infobox\u Unternehmen”]”)
如果不是行:
rows=resp.xpath('.//*[@id=“Vorlage\u Infobox\u Unternehmen”]//表')
尝试:
如果是行:
extract=lambda行,路径:row.xpath(path.extract_first().strip()
如果在str(行)中有“”:
infobox={extract(行,'string(./th)'):为行中的行提取(行,'string(./td)}
str(行)中的elif“”
infobox={extract(row,'string(./td[1])):为行中的行提取(row,'string(./td[2]))
str(行)中的elif“”
infobox={extract(行,'string(./th)'):为行中的行提取(行,'string(./td)}
其他:
infobox={extract(row,'string(./table/tbody/tr[1])):为行中的行提取(row,'string(./td[1]))

但我做错了什么,得不到我想要的。请帮助我理解我的错误。

如果您想在
中获取
的值,可以在xpath上执行以下操作:

    table = resp.xpath('//table[@id="Vorlage_Infobox_Unternehmen"]')
    if table:
        all_table_data = table.xpath('//td')
当您使用
table.xpath('some_xpath')
时,它会将其应用于已选择的元素。您也可以跳过该测试,直接执行:

    all_table_data = resp.xpath('//table[@id="Vorlage_Infobox_Unternehmen"]//td')

如果您指定预期的结果,将更容易帮助您为什么不使用行选择器,然后解析每一行并将值添加到信息框中?