python-从第二组链接中抓取,抓取页面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-从第二组链接中抓取,抓取页面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4533字,纯文字阅读大概需要7分钟。
内容图文
![python-从第二组链接中抓取,抓取页面](/upload/InfoBanner/zyjiaocheng/690/f233892b67874eef9980682c7abb0f6a.jpg)
我今天一直在浏览Scrapy文档,并尝试在一个真实的示例中获得-https://docs.scrapy.org/en/latest/intro/tutorial.html#our-first-spider的工作版本.我的示例稍有不同,因为它有2个下一页,即
start_url > city page > unit page
这是我要从中获取数据的单位页面.
我的代码:
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)
因此,我认为我的代码未设置为检索上述流程中的链接,但不确定如何做到最好?
更新流程:
Main page > City page > Building page > Unit page
仍然是我要从中获取数据的单位页面.
更新的代码:
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)
解决方法:
让我们从逻辑开始:
>抓取首页-获取所有城市
>抓取城市页面-获取所有单元网址
>抓取单元页面-获取所有所需数据
我已经举了一个例子,说明如何在下面的抓蜘蛛中实现这一点.我找不到您在示例代码中提到的所有信息,但是希望该代码足够清晰,以使您了解它的作用以及如何添加所需的信息.
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循环.如果不清楚,请告诉我,我将尽力解释.
内容总结
以上是互联网集市为您收集整理的python-从第二组链接中抓取,抓取页面全部内容,希望文章能够帮你解决python-从第二组链接中抓取,抓取页面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。