Python 刮,从第二组链接中刮取页面

Python 刮,从第二组链接中刮取页面,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,今天,我一直在浏览这些残缺不全的文档,并试图在一个真实的例子中得到一个有效的版本。我的示例稍有不同,它有两个后续页面,即 开始\u url>城市页面>单位页面 这是我想要从中获取数据的单元页面 我的代码: import scrapy class QuotesSpider(scrapy.Spider): name = "quotes" start_urls = [ 'http://www.unitestudents.com/', ]

今天,我一直在浏览这些残缺不全的文档,并试图在一个真实的例子中得到一个有效的版本。我的示例稍有不同,它有两个后续页面,即

开始\u url>城市页面>单位页面

这是我想要从中获取数据的单元页面

我的代码:

import scrapy


class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://www.unitestudents.com/',
            ]

    def parse(self, response):
        for quote in response.css('div.property-body'):
            yield {
                'name': quote.xpath('//span/a/text()').extract(),
                'type': quote.xpath('//div/h4/text()').extract(),
                'price_amens': quote.xpath('//div/p/text()').extract(),
                'distance_beds': quote.xpath('//li/p/text()').extract()
            }

            # Purpose is to crawl links of cities
            next_page = response.css('a.listing-item__link::attr(href)').extract_first()
            if next_page is not None:
                next_page = response.urljoin(next_page)
                yield scrapy.Request(next_page, callback=self.parse)

            # Purpose is to crawl links of units
            next_unit_page = response.css(response.css('a.text-highlight__inner::attr(href)').extract_first())
            if next_unit_page is not None:
                                          next_unit_page = response.urljoin(next_unit_page)
                                          yield scrapy.Request(next_unit_page, callback=self.parse)
但当我运行这个时,我得到:

INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
因此,我认为我的代码没有设置为检索上面提到的流中的链接,但不确定如何最好地做到这一点

更新流程:

主页>城市页面>建筑页面>单元页面

它仍然是我想要从中获取数据的单位页面

更新代码:

import scrapy


class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://www.unitestudents.com/',
            ]

    def parse(self, response):
        for quote in response.css('div.site-wrapper'):
            yield {
                'area_name': quote.xpath('//div/ul/li/a/span/text()').extract(),
                'type': quote.xpath('//div/div/div/h1/span/text()').extract(),
                'period': quote.xpath('/html/body/div/div/section/div/form/h4/span/text()').extract(),
                'duration_weekly': quote.xpath('//html/body/div/div/section/div/form/div/div/em/text()').extract(),
                'guide_total': quote.xpath('//html/body/div/div/section/div/form/div/div/p/text()').extract(),              
                'amenities': quote.xpath('//div/div/div/ul/li/p/text()').extract(),              
            }

            # Purpose is to crawl links of cities
            next_page = response.xpath('//html/body/div/footer/div/div/div/ul/li/a[@class="listing-item__link"]/@href').extract()
            if next_page is not None:
                next_page = response.urljoin(next_page)
                yield scrapy.Request(next_page, callback=self.parse)

            # Purpose is to crawl links of units
            next_unit_page = response.xpath('//li/div/h3/span/a/@href').extract()
            if next_unit_page is not None:
                                          next_unit_page = response.urljoin(next_unit_page)
                                          yield scrapy.Request(next_unit_page, callback=self.parse)

            # Purpose to crawl crawl pages on full unit info

            last_unit_page = response.xpath('//div/div/div[@class="content__btn"]/a/@href').extract()
            if last_unit_page is not None:
                last_unit_page = response.urljoin(last_unit_page)
                yield scrapy.Request(last_unit_page, callback=self.parse)

让我们从逻辑开始:

  • 刮主页-获取所有城市
  • 刮城市页面-获取所有单元URL
  • 刮取单元页面-获取所有所需数据
  • 下面我举了一个例子,说明了如何在一个刮擦蜘蛛中实现这一点。我无法找到您在示例代码中提到的所有信息,但我希望代码足够清晰,让您能够理解它的作用以及如何添加所需的信息

    import scrapy
    
    
    class QuotesSpider(scrapy.Spider):
        name = "quotes"
        start_urls = [
            'http://www.unitestudents.com/',
                ]
    
        # Step 1
        def parse(self, response):
            for city in response.xpath('//select[@id="frm_homeSelect_city"]/option[not(contains(text(),"Select your city"))]/text()').extract(): # Select all cities listed in the select (exclude the "Select your city" option)
                yield scrapy.Request(response.urljoin("/"+city), callback=self.parse_citypage)
    
        # Step 2
        def parse_citypage(self, response):
            for url in response.xpath('//div[@class="property-header"]/h3/span/a/@href').extract(): #Select for each property the url
                yield scrapy.Request(response.urljoin(url), callback=self.parse_unitpage)
    
            # I could not find any pagination. Otherwise it would go here.
    
        # Step 3
        def parse_unitpage(self, response):
            unitTypes = response.xpath('//div[@class="room-type-block"]/h5/text()').extract() + response.xpath('//h4[@class="content__header"]/text()').extract()
            for unitType in unitTypes: # There can be multiple unit types so we yield an item for each unit type we can find.
                yield {
                    'name': response.xpath('//h1/span/text()').extract_first(),
                    'type': unitType,
                    # 'price': response.xpath('XPATH GOES HERE'), # Could not find a price on the page
                    # 'distance_beds': response.xpath('XPATH GOES HERE') # Could not find such info
                }
    

    我认为代码非常干净和简单。注释应该澄清我为什么选择使用for循环。如果有什么不清楚的地方,请告诉我,我会尽力解释。

    您是否使用
    scrapy shell测试了您的XPathhttp://www.unitestudents.com/
    例如?@paultrmbrth我对它们进行了测试,但发现了一些缺陷,所以我做了一些更改-请参阅更新。你的想法是什么?谢谢你的帮助!我想知道这是从哪里来的-
    //div[@class=“room type block”]/h5/text()
    ?我似乎在任何一页上都找不到它。事实证明,我只取了一个单元页,假设所有其他单元页都是一样的,但事实并非如此(事实上,这个页面更像是一个例外)。我用过,但你是对的;我在很多网站上都找不到这个元素。我已经更新了代码来处理这两个页面。(注意:我没有测试完整的代码)谢谢你,工作得很好!我只是想知道,为什么这个部分-
    callback=self.parse\u citypage
    -指向下一个函数?对不起,这感觉这是一个RTFM问题!不确定我是否理解您的困惑,但您告诉Scrapy获取指定的url,并告诉它在获取数据时,应该使用获取的响应数据调用指定的回调函数。如果代码回答了您的问题,请检查此答案是否已被接受。