Как пропустить, если href нет? - PullRequest
0 голосов
/ 06 октября 2018

Я анализирую страницу, у которой есть 20 ссылок на следующую страницу.Вот так: enter image description here

Но у одного из них нет href enter image description here

Это вызовет мой кодошибка.

    i = 1000
    j = 0
    dataLen = len(response.xpath('//div[@class="rank_list table rankstyle1"]//div[@class="tr"]'))
    photoNodes = response.xpath('//div[@class="rank_list table rankstyle1"]//div[@class="tr"]')
    for photoNode in photoNodes:
        contentHref = photoNode.xpath('.//a/@href').extract_first()
        yield Request(contentHref, callback=self.parse_page, priority = i, dont_filter=True)
        i -= 1
        j += 1  
    # start parse next page
    def parse_page(self, response):       
        global countLen, dataLen
        enName = response.xpath('//*[@class="movie_intro_info_r"]/h3/text()').extract_first()
        cnName = response.xpath('//*[@class="movie_intro_info_r"]/h1/text()'
        ...

Я пытаюсь добавить if not (photoNode is None): или if not photoNode =="", все еще не работающий.

i = 1000
j = 0
dataLen = len(response.xpath('//div[@class="rank_list table rankstyle1"]//div[@class="tr"]'))
photoNodes = response.xpath('//div[@class="rank_list table rankstyle1"]//div[@class="tr"]')
for photoNode in photoNodes:
    if not (photoNode is None):
        contentHref = photoNode.xpath('.//a/@href').extract_first()
        # photoHref = photoNode.xpath('.//a/img/@src').extract_first()
        yield Request(contentHref, callback=self.parse_page, priority = i, dont_filter=True)
        i -= 1
        j += 1  
    else:
        pass
twRanking['movie'] = movieArray

Я понятия не имею, как его пропустить, если он может не иметь href.

Буду признателен за любую помощь.Заранее спасибо.

1 Ответ

0 голосов
/ 06 октября 2018

Кажется, что вам нужно проверить, не является ли contentHref не пустым, а не photoNode.photoNode в любом случае будет содержать информацию, поэтому она не будет пустой.Попробуйте что-то вроде этого:

for photoNode in photoNodes:
    contentHref = photoNode.xpath('.//a/@href').extract_first()
    if contentHref:
        # photoHref = photoNode.xpath('.//a/img/@src').extract_first()
        yield Request(contentHref, callback=self.parse_page, priority = i, dont_filter=True)
        i -= 1
        j += 1  
    else:
        pass
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...