代码来自《Learning Scrapy》, 还在学习当中,这段代码基本是抄的,想运行一下,看看效果,无奈没有达到书中所描述的效果, 就是并没有跳转到下一页进行爬虫,帮忙看一下.
代码:
import datetime import urlparse import socket from scrapy.loader.processors import MapCompose, Join from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule from scrapy.loader import ItemLoader from ..items import PropertiesItem class EasySpider(CrawlSpider): name = 'easy' allowed_domains = ['http://192.168.99.100:32768/'] start_urls = ['http://192.168.99.100:32768/properties/index_00000.html',] # Rules for horizontal and vertical crawling rules = ( Rule(LinkExtractor(restrict_xpaths='//*[contains(@class,"next")]')), Rule(LinkExtractor(restrict_xpaths='//*[@itemprop="url"]'), callback='parse_item') ) def parse_item(self, response): """ This function parses a property page. @url http://192.168.99.100:32768/properties/property_000000.html @returns items 1 @scrapes title price description address image_urls @scrapes url project spider server date """ # Create the loader using the response l = ItemLoader(item=PropertiesItem(), response=response) # Load fields using XPath expressions l.add_xpath('title', '//*[@itemprop="name"][1]/text()', MapCompose(unicode.strip, unicode.title)) l.add_xpath('price', './/*[@itemprop="price"][1]/text()', MapCompose(lambda i: i.replace(',', ''), float), re='[,.0-9]+') l.add_xpath('description', '//*[@itemprop="description"][1]/text()', MapCompose(unicode.strip), Join()) l.add_xpath('address', '//*[@itemtype="http://schema.org/Place"][1]/text()', MapCompose(unicode.strip)) l.add_xpath('image_urls', '//*[@itemprop="image"][1]/@src', MapCompose(lambda i: urlparse.urljoin(response.url, i))) # Housekeeping fields l.add_value('url', response.url) l.add_value('project', self.settings.get('BOT_NAME')) l.add_value('spider', self.name) l.add_value('server', socket.gethostname()) l.add_value('date', datetime.datetime.now()) return l.load_item()
上面的爬虫地址是docker中的
网页源代码为:
Scrapy Book Tutorial Example
Page 4
- station split fee i triple, price: £881.0pw location: Wimbledon, London while value bathrooms famous ucl towards lounge both but buses croydon heating square stoke floors maisonette local
- european station area royal food, price: £1151.0pw location: Battersea, London home sw questions today de look hampstead drawer way have bayswater team tower hr hyde
- ontario maintained rm bbc modern, price: £1026.0pw location: Heathrow, London quick views waterloo ensuite elephant moments loads stylish pounds hoxton notting min cosy boasting dinning opportunity integrated mid want london
- jacuzzi marble newbury also wanted, price: £1096.0pw location: South Kensington, London your bit email located park westfield
- woolwich be students gants smart, price: £626.34pw location: Seven Sisters, London sainsbury self canada newington students quality possible built corner
- soon tottenham highgate excel decorated, price: £1161.0pw location: Bayswater, London charges suit live post diner doors dedicated http british leyton property pleased link included month sky number mod fulham breakfast carpeted
- throughout holiday roehampton location ref, price: £833.0pw location: Battersea, London stop very offer really vale tube video pretty benefits brixton others walthamstow including pw car keep brixton questions anytime days house theatre fulham victorian maida
- delightful islington storage fulham basement, price: £771.88pw location: Seven Sisters, London self beautifully newington
- clapton wifi island stay prime, price: £1192.0pw location: Islington, London now wi unlimited stores minimum nearby international center superb benefit when inclusive manned court
- young ladies space few barnet bright best smart, price: £1076.0pw location: Hammersmith, London quick paddington mobile square imperial pleased west looking shop street its benefits development direct minutes enjoy creative boasts access london direct east
- prime k required commercial hammersmith be hayes john, price: £486.85pw location: Shepherds Bush, London soon stoke safe open wardrobes agent apartments french letting
- upton than outstanding colindale kensal shared junction stepney, price: £1029.0pw location: West Hampstead, London pw price beautifully massive wharf available approx style than
- royal non transport buses palmers last quick suite, price: £884.0pw location: Stratford, London is popular student paddington station tv hr popular library church
- immaculate edgware guy twin warehouse angel views georgian, price: £797.0pw location: Greenwich, London mod eat at have able send whitechapel dss open spaces la own french following whole cosy meeting comprises pets piccadilly town
- leyton distance from suite clerkenwell uni jubilee tulse, price: £735.0pw location: Angel, London enquiries showers travel suit fast reference from
- centre camden chelsea angel bridge stoke cheap required, price: £1151.4pw location: Chelsea, London services include close own these ceilings tiled throw fridge interior
- th to meg upton leytonstone kingsbury check chelsea, price: £785.0pw location: Camden Town, London within suit tenant convenient gov four friends uk these double richmond utility main either walthamstow stratford square stoke my us ride
- rooms northern massive business kingsbury spec beautiful camden, price: £1017.0pw location: Hyde Park, London bit housemates built business professionals to http together rd booking imperial day zone move metropolitan bathroom friendly come monthly at bath
- se newbury clapton beauty superb within royal lots, price: £1052.87pw location: Whitechapel, London then as o follow nw only system prices fast
- apartment mitcham available its soon accomodation wanstead extremely, price: £1248.0pw location: Shoreditch, London piccadilly work cafes fee laundry benefit contract house kensington them
- star charming x wick, price: £1290.0pw location: Bethnal Green, London system rates hospital visit current ride well walk british place you tenancy
- deal looking circus superb, price: £897.0pw location: Islington, London uk for tesco square keep marble victorian could tenants kings south takes from request boasting going when connected through o electricity lift tube sainsbury quote full broadway offered value
- hotel ec first notting, price: £787.0pw location: Camden Town, London fashion second walking screen quay unlimited within
- tulse n mews summer, price: £1177.0pw location: Camden, London french finchley thames safe weekly opposite we house viewings providing week open ad
- rise furn promotion heating, price: £738.0pw location: Shoreditch, London home who miles liverpool require area westfield which nearest available our restaurant boasts much en early
- mansions caledonian the quite, price: £703.8pw location: Croydon, London wardrobe leading seconds covent garden wood sought ground smoker notice boasting needed email ll period guy sorry bbq thank refurbished different licence contemporary converted during block itself
- caledonian service spectacular convenient, price: £1184.0pw location: Tower Hamlets, London during opposite bill pay letting bethnal
- waterloo condition knightsbridge rentals, price: £877.85pw location: Tower Bridge, London current free nd professional overlooking support most there link
- university mile contract room, price: £892.0pw location: Brixton, London professional king not room plus coming many so d following rent professionals seconds washer tax check french guys hyde offered conditioning interested
- accommodation st russell vale, price: £1246.39pw location: Highbury, London anytime workstation museum highly wardrobe both speak attractions residential based avoid stoke most be buses charges team away min moments moving move apartments pay right lines trains
两个rule交换一下顺序试试