最近在學習scrapy爬蟲,正好最近成都房價漲的厲害识腿,于是想著去網(wǎng)上抓抓成都最近的房價情況,順便了解一下造壮,畢竟咱是成都人渡讼,得看看這成都的房子我以后買的起不~
話不多說,進入正題:
任務
抓取鏈家網(wǎng)(成都)的所有新房以及二手房價格耳璧、位置相關信息成箫。
實驗流程
1.確定抓取目標.
2.制定抓取規(guī)則.
3.'編寫/調試'抓取規(guī)則.
4.獲得抓取數(shù)據(jù)
1.確定抓取目標
1.1新房抓取目標
我們看到在成都樓盤列表頁面,有樓盤名字旨枯、均價(每平方)和所屬行政區(qū)蹬昌,非常好,這正是我們想要抓取的內容攀隔!
于是確定目標:
抓取成都新房的所有樓盤名稱皂贩、均價信息、所屬行政區(qū)和URL信息昆汹。
1.2二手房抓取規(guī)則
我們來到二手房的列表頁面先紫,并且點擊進入二手房詳情頁面,發(fā)現(xiàn)網(wǎng)站所展示的內容就比新房的內容的多得多了筹煮,這樣我們也能抓取更多的數(shù)據(jù)了!在這里居夹,我決定在二手房頁面抓取二手房地址败潦、總價本冲、每平米價、面積劫扒、所屬行政區(qū)域和URL信息檬洞。
2.制定抓取規(guī)則
2.1新房抓取規(guī)則
由于新房的信息直接在新房列表網(wǎng)頁上就能直接抓取,所以只需要在新房列表網(wǎng)頁分析html結構找到我們想要的數(shù)據(jù)即可沟饥。
對于均價來說添怔,我們所需要的信息在 **//div[@class='average']/span/text() **
對于樓盤名稱來說,我們所需要的信息在 //div[@class='col-1']/h2/a/text()
當然了贤旷,我們所需要的URL也在 //div[@class='col-1']/h2/a/@href
對于行政區(qū)來說广料,所需要的信息在 //div[@class='where']/span/text(),當然了幼驶,我們還得使用正則將所屬行政區(qū)匹配出來
如何抓取所有的新房信息呢艾杏?我們利用下一頁來循環(huán)
2.2二手房抓取規(guī)則
抓取規(guī)則和新房抓取規(guī)則大同小異,不過這里有一點小小的區(qū)別是我們需要利用二手房列表頁面抓取到的URL盅藻,進入二手房詳情頁抓取信息购桑,不過這一點我們將在代碼實現(xiàn)中講到,在這兒先按住不表氏淑,還是繼續(xù)分析html結構勃蜘。
小區(qū)名稱在 div[@class='communityName']/a[@class='info']/text()
房總價在 div[@class='price ']/span[@class='total']/text()
每平方價在 div[@class='price ']/div[@class='text']/div/span/text() ,當然這里我們還是得用正則把兩個引號給弄掉~
所屬行政區(qū)在 div[@class='areaName']/span[@class='info']/a/text()的第一個text
面積在 div[@class='houseInfo']/div[@class='area']/div[@class='mainInfo']/text()
當然了,對于循環(huán)獲得所有信息來說假残,我們還是用網(wǎng)站的“下一頁”來做缭贡。
3.'編寫/調試'抓取規(guī)則
為了讓調試爬蟲的粒度盡量的小,我將編寫和調試模塊糅合在一起進行.
3.1'編寫/調試'新房抓取規(guī)則
首先我們得定義一個item:
class NewHoseItem(scrapy.Item):
name = scrapy.Field()
average = scrapy.Field()
district = scrapy.Field()
URL = scrapy.Field()
然后我們嘗試抓取一個新房列表頁面下的所需數(shù)據(jù)吧~
import scrapy
from fangjia.items import NewHouseItem
class NewsHouseSpider(scrapy.Spider):
name = "new"
start_urls = [
"http://cd.fang.lianjia.com/loupan/pg1/",
]
def parse(self, response):
for href in response.xpath("http://div[@class='info-panel']"):
item = NewHouseItem()
item['name'] = href.xpath("div[@class='col-1']/h2/a/text()").extract_first()
item['average'] = href.xpath("div[@class='col-2']/div[@class='price']/div[@class='average']/span/text()").extract_first()
item['district'] = href.xpath("div[@class='col-1']/div[@class='where']/span/text()").extract_first().split('-')[0]
item['URL'] = response.urljoin(href.xpath("div[@class='col-1']/h2/a/@href").extract_first())
yield item
測試通過!
現(xiàn)在我們再編寫一下循環(huán)的規(guī)則就ok了守问!
由于我們需要獲取到totalpage的值和curpage的值匀归,所以我們得對獲取到的字符串稍微的處理一下,這里我們用到的是split()
next_page = response.xpath("http://div[@class='page-box house-lst-page-box']/@page-data").extract_first().split(',')[1].split(':')[1]
next_page = int(next_page[0])+1
total_page = int(response.xpath("http://div[@class='page-box house-lst-page-box']/@page-data").extract_first().split(',')[0].split(':')[1])
if next_page <= total_page:
next_urls = 'pg%s/' % next_page
next_urls = response.urljoin(next_urls)
yield scrapy.Request(next_urls,callback = self.parse)
組裝到我們的原函數(shù)里面
import scrapy
from fangjia.items import NewHouseItem
class NewsHouseSpider(scrapy.Spider):
name = "new"
start_urls = [
"http://cd.fang.lianjia.com/loupan/",
]
def parse(self, response):
for href in response.xpath("http://div[@class='info-panel']"):
item = NewHouseItem()
item['name'] = href.xpath("div[@class='col-1']/h2/a/text()").extract_first()
item['average'] = href.xpath("div[@class='col-2']/div[@class='price']/div[@class='average']/span/text()").extract_first()
item['district'] = href.xpath("div[@class='col-1']/div[@class='where']/span/text()").extract_first().split('-')[0]
item['URL'] = href.xpath("div[@class='col-1']/h2/a/@href").extract_first()
yield item
next_page = response.xpath("http://div[@class='page-box house-lst-page-box']/@page-data").extract_first().split(',')[1].split(':')[1]
next_page = int(next_page[0])+1
total_page = int(response.xpath("http://div[@class='page-box house-lst-page-box']/@page-data").extract_first().split(',')[0].split(':')[1])
if next_page <= total_page:
next_urls = 'pg%s/' % next_page
next_urls = response.urljoin(next_urls)
yield scrapy.Request(next_urls,callback = self.parse)
測試耗帕,成功穆端!
3.2'編寫/調試'二手房抓取規(guī)則
二手房抓取規(guī)則和新房差不多,所以寫簡單一點仿便,其中主要說明一點是利用二手房列表頁面抓取到的URL体啰,進入二手房詳情頁抓取信息。
先定義item:
class OldHouseItem(scrapy.Item):
name = scrapy.Field()
total = scrapy.Field()
average = scrapy.Field()
size = scrapy.Field()
district = scrapy.Field()
URL = scrapy.Field()
當然了嗽仪,我們還是先寫出一個二手房列表頁面的抓取規(guī)則荒勇,其中,得利用抓取到的URL再進入二手房詳情頁進行抓取闻坚。
編寫爬蟲:
import scrapy
from fangjia.items import OldHouseItem
class OldsHouseSpider(scrapy.Spider):
name = "old"
start_urls = [
"http://cd.lianjia.com/ershoufang/",
]
def parse(self, response):
for href in response.xpath("http://ul[@class='sellListContent']/li"):
URL = href.xpath("div[@class='info clear']/div[@class='title']/a/@href").extract()
yield scrapy.Request(URL, callback=self.parse_dir_contents)
def parse_dir_contents(self, response):
item = OldHouseItem()
item['name'] = response.xpath("div[@class='communityName']/a[@class='info']/text()").extract_first()
item['total'] = response.xpath("div[@class='price ']/span[@class='total']/text()").extract_first()
item['average'] = response.xpath("div[@class='price ']/div[@class='text']/div/span/text()").extract_first()
item['size'] = response.xpath("div[@class='houseInfo']/div[@class='area']/div[@class='mainInfo']/text()").extract_first()
item['district'] = response.xpath("div[@class='areaName']/span[@class='info']/a/text()").extract_first()
item['URL'] = response
yield item
發(fā)現(xiàn)有重定向:
一看沽翔,是robot的原因,于是我們進入 http://cd.lianjia.com/robots.txt 一探究竟
在robots.txt最下面看到
User-agent:*
Disallow:/
原來我們的scrapy被屏蔽了!
當然仅偎,我們還有辦法跨蟹,不過筆者得去吃午飯了!晚上再回來研究研究~