【问题标题】:wrong Xpath in IMDB spider scrapyIMDB蜘蛛scrapy中的错误Xpath
【发布时间】:2017-07-14 15:31:24
【问题描述】:

这里: IMDB scrapy get all movie data

response.xpath("//*[@class='results']/tr/td[3]")

返回空列表。我尝试将其更改为:

response.xpath("//*[contains(@class,'chart full-width')]/tbody/tr")

没有成功。

有什么帮助吗?谢谢。

【问题讨论】:

  • 当问题发生时,您能否指定您正在从哪个链接中抓取?
  • 可以,例如:imdb.com/search/…
  • 我不确定你想在这里做什么。但我检查了网站,没有带有class resultschart full-width 的 xpath
  • 我从链接 stackoverflow.com/questions/35819404/… 中获取了 xpath 我的目标是让 'parse_page' 功能正常工作。
  • “我的目标是让 'parse_page' 功能工作。” 不是很明确。请分享您期望从某些示例 URL 的回调中获得的输出。

标签: python xpath scrapy


【解决方案1】:

我没有时间仔细阅读IMDB scrapy get all movie data,但已经掌握了要点。问题陈述是从给定站点获取所有电影数据。它涉及两件事。 首先是浏览包含当年所有电影列表的所有页面。 第二个是获取每部电影的链接,然后在这里您可以发挥自己的魔力。

您面临的问题是获取每部电影链接的 xpath。这很可能是由于网站结构的变化(我没有时间验证可能有什么不同)。无论如何,以下是您需要的xpath


第一:

我们以divnav 为标志,并在其子类中找到lister-page-next next-page 类。

response.xpath("//div[@class='nav']/div/a[@class='lister-page-next next-page']/@href").extract_first()

这里会给出:下一页的链接 |如果在最后一页返回None(因为没有下一页标签)


第二:

这是OP最初的疑问。

#Get the list of the container having the title, etc
list = response.xpath("//div[@class='lister-item-content']")

#From the container extract the required links 
paths = list.xpath("h3[@class='lister-item-header']/a/@href").extract()

现在您需要做的就是遍历每个paths 元素并请求页面。


【讨论】:

    【解决方案2】:

    感谢您的回答。我最终像这样使用了你的 xPath:

    import scrapy
    from scrapy.spiders import CrawlSpider, Rule
    from scrapy.linkextractors import LinkExtractor
    
    from crawler.items import MovieItem
    
    IMDB_URL = "http://imdb.com"
    
    class IMDBSpider(CrawlSpider):
        name = 'imdb'
        # in order to move the next page
        rules = (Rule(LinkExtractor(allow=(), restrict_xpaths=("//div[@class='nav']/div/a[@class='lister-page-next next-page']",)),
                      callback="parse_page", follow= True),)
    
        def __init__(self, start=None, end=None, *args, **kwargs):
            super(IMDBSpider, self).__init__(*args, **kwargs)
            self.start_year = int(start) if start else 1874
            self.end_year = int(end) if end else 2017
    
        # generate start_urls dynamically
        def start_requests(self):
            for year in range(self.start_year, self.end_year+1):
                # movies are sorted by number of votes
                yield scrapy.Request('http://www.imdb.com/search/title?year={year},{year}&title_type=feature&sort=num_votes,desc'.format(year=year))
    
        def parse_page(self, response):
            content = response.xpath("//div[@class='lister-item-content']")
            paths = content.xpath("h3[@class='lister-item-header']/a/@href").extract() # list of paths of movies in the current page
    
            # all movies in this page
            for path in paths:
                item = MovieItem()
                item['MainPageUrl'] = IMDB_URL + path
                request = scrapy.Request(item['MainPageUrl'], callback=self.parse_movie_details)
                request.meta['item'] = item
                yield request
    
        # make sure that the start_urls are parsed as well
        parse_start_url = parse_page
    
        def parse_movie_details(self, response):
            pass # lots of parsing....
    

    使用scrapy crawl imdb -a start=<start-year> -a end=<end-year> 运行它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-20
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多