Scrapy:如何使用条件语句在 table 中抓取 link

Scrapy: how to scrape a link inside a table with conditionals

我是 python 和 scrapy 的新手,我必须抓取一个完全由 tables(将近 80 tables)构建的网站。

网站的结构是这样的:

<table>
<tr>
<td class="header" colspan="2">something</td>
</tr>

</table>
<br/>
<table> 
<tr>
<td class="header" colspan="2">something2</td>
</tr>

</table>
<br/>
<table>
<tr> 
<td class="header" colspan="2">something3</td>
</tr>
</table>

但是在其中一个 table 中有一个成员列表,我需要提取每个成员的个人资料信息,但是每个个人资料都是可变的,所以 table 与它的信息会根据隐私设置而变化。

我需要抓取的table是这样的,但是有很多成员:

<table>
            <tr>
                <td colspan="4" class="header">members</td>
            </tr>
            <tr>
                <td class="title">Name</td>
                <td class="title">position</td>
                <td class="title">hours</td>
                <td class="title">observ</td>
            </tr>

            <tr>
                <td class="c1">       
                    1.- <a href="http://profiletype1" target="_blank">Homer Simpson</a>
                </td>
                <td class="c1">
                    safety inspector
                </td>
                <td class="c1">
                    10
                </td>
                <td class="c1">
                    Neglect his duties
                </td>
            </tr>
<table>

然后我查看了代码,我注意到有两种类型的配置文件,使用 xpath 的查询不会相互交叉。

然后问题是我如何提取每个成员的个人资料信息,考虑到当我打开 link 我可以找到两种不同类型的个人资料。我想我需要一个代码来做这样的事情

def parse(self, response):
if this xpath query doesn't work
try this one

我认为您已经回答了您的问题,并且该解决方案非常针对特定领域,因此我能够给出正确的答案。不管怎样,我会尽量让你知道我将如何解决这个问题。

def parse(self, respose):
    test = response.xpath("//some expression that only works in method one").extract_first()
    if test is not None:
      return self.parse_with_method_one(response)
    return self.parse_with_method_two(response)

def parse_with_method_one(self, response):
    # your logic

def parse_with_method_two(self, response):
    # your logic