用scrapy提取缺失值

问题描述 投票:0回答:1

我试图解决在链接和critical项目中使用item_loader.add_css处理缺失值的问题。每个项目应该是8个值,但在critical中是6个,而且没有提取任何东西。

我的代码是:

rotten_spyder.py

class RottenSpiderSpider(scrapy.Spider):

    name = 'rotten'
    start_urls = ['https://www.rottentomatoes.com/m/toy_story/reviews?type=top_critics&sort=&page=2']

    def parse(self, response):
        # crawl page 
        for row in response.css('.content'):

            item_loader = ItemLoader(item=ScraperottentomatoesItem(), selector=row)
            item_loader.add_css('quote', '.the_review::text', re='\w+.+')
            item_loader.add_css('links', '.review-link a::attr(href)')
            item_loader.add_css('critic', '#content .articleLink::text')

            yield item_loader.load_item()

items.py

class ScraperottentomatoesItem(scrapy.Item):
    quote = scrapy.Field()
    links = scrapy.Field()
    critic = scrapy.Field()

这是不含critical的输出。

enter image description here

enter image description here

python scrapy
1个回答
0
投票

对于第一部分你说应该是8个项目。如果你看看网站上的超链接,上面写着 "完整评论",那就是链接的位置。只有6个链接,所以这很好。对于第二部分,你是提取文本而不是链接,所以你会像其他部分一样改变它。

这里做这个代码。它为我工作。

Rotten

下面是输出结果。

{'critor': ['Sean P. Means', 'John Hartl', 'Peter Stack', 'Roger Ebert', 'Rita Kempley', 'James Berardinelli', 'Roger Moore'], 'link': ['http:/www.sfgate.comcgi-binarticle.cgi?f=ca19961101DD69735.DTL', 'http:/www.rogerebert.comreviewstoy-story-1995', 'http:/www.washingtonpost.comwp-srvstylelongtermmoviesvideostoystory.htm', 'http:/www.reelviews.netphp_review_template.php?identifier=46', 'http:/www.ew.comewarticle0,,299671,00.html', 'http:/www.orlandosentinel.comentertainmentmoviesorl-movie-review-toy-story-toy-story-2-3d,0,4640680.story'], 'quote': ['Technically, Toy Story is nearly flawless.', "Disney's witty, wondrously imaginative, all-computer-generated " 'cartoon is far and away the best of the new holiday movies in ''town.', 'The script, by Lasseter, Pete Docter, Andrew Stanton and Joe' 'Ranft, is filled clever gags keep the two heroes at each ''other's throats and Joe' 'Ranft', ''the new holiday movies in ''town. ','剧本,由拉塞特、皮特-多克特、安德鲁-斯坦顿和乔''兰夫特,充满了聪明的插科打诨,让两个英雄在每个''对方的喉咙和快进的情节。","结果是一个有远见的过山车的电影。 ',"看到电影公司从这个"'关于20世纪普通孩子安迪和他的''急于取悦的玩物的现代故事中汲取魔力,这是一个很好的节奏变化。","关于《玩具总动员》的一个大的负面因素涉及迪士尼的"'过度商业化。 ','我很难想象在电影院里能比在'"玩具总动员 "里享受到更多的乐趣,这部神奇的迪斯尼新片是第一部""完全用电脑制作的全长动画电影。","出色的配音表演,视觉上的双关语,所有这些加起来就是一部'"动画游戏的改变者。"]}。

© www.soinside.com 2019 - 2024. All rights reserved.