猿问

从表中提取(Scrapy)

想在python2中使用scrapy进行表解析时寻求帮助,这是我的表: 链接到表 我需要获取<td>标签的值。尝试使用下一个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}

但是我做错了事,无法得到我的魔杖。请帮助我理解我的错误。


郎朗坤
浏览 134回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答