【发布时间】:2017-04-19 10:52:59
【问题描述】:
我今天一直在浏览 Scrapy 文档,并试图在一个真实的例子中获得一个工作版本 - https://docs.scrapy.org/en/latest/intro/tutorial.html#our-first-spider。我的示例略有不同,因为它有 2 个下一页,即
start_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)
【问题讨论】:
-
您是否使用
scrapy shell http://www.unitestudents.com/测试过您的 XPath? -
@paultrmbrth 我已经对它们进行了测试,但发现了一些缺陷,所以我做了一些更改 - 请参阅更新。你有什么想法?
标签: python web-scraping scrapy