自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python爬取房產(chǎn)數(shù)據(jù),在地圖上展現(xiàn)!

開發(fā) 后端 數(shù)據(jù)分析
小伙伴,我又來了,這次我們寫的是用python爬蟲爬取烏魯木齊的房產(chǎn)數(shù)據(jù)并展示在地圖上,地圖工具我用的是 BDP個(gè)人版-免費(fèi)在線數(shù)據(jù)分析軟件,數(shù)據(jù)可視化軟件 ,這個(gè)可以導(dǎo)入csv或者excel數(shù)據(jù)。

Python爬取房產(chǎn)數(shù)據(jù),在地圖上展現(xiàn)!

小伙伴,我又來了,這次我們寫的是用python爬蟲爬取烏魯木齊的房產(chǎn)數(shù)據(jù)并展示在地圖上,地圖工具我用的是 BDP個(gè)人版-免費(fèi)在線數(shù)據(jù)分析軟件,數(shù)據(jù)可視化軟件 ,這個(gè)可以導(dǎo)入csv或者excel數(shù)據(jù)。

  • 首先還是分析思路,爬取網(wǎng)站數(shù)據(jù),獲取小區(qū)名稱,地址,價(jià)格,經(jīng)緯度,保存在excel里。再把excel數(shù)據(jù)上傳到BDP網(wǎng)站,生成地圖報(bào)表

本次我使用的是scrapy框架,可能有點(diǎn)大材小用了,主要是剛學(xué)完用這個(gè)練練手,再寫代碼前我還是建議大家先分析網(wǎng)站,分析好數(shù)據(jù),再去動(dòng)手寫代碼,因?yàn)楹玫姆治隹梢允掳牍Ρ?,烏魯木齊樓盤,2017烏魯木齊新樓盤,烏魯木齊樓盤信息 - 烏魯木齊吉屋網(wǎng) 這個(gè)網(wǎng)站的數(shù)據(jù)比較全,每一頁獲取房產(chǎn)的LIST信息,并且翻頁,點(diǎn)進(jìn)去是詳情頁,獲取房產(chǎn)的詳細(xì)信息(包含名稱,地址,房價(jià),經(jīng)緯度),再用pipelines保存item到excel里,最后在bdp生成地圖報(bào)表,廢話不多說上代碼:

JiwuspiderSpider.py

  1. # -*- coding: utf-8 -*- 
  2. from scrapy import Spider,Request 
  3. import re 
  4. from jiwu.items import JiwuItem 
  5.  
  6.  
  7. class JiwuspiderSpider(Spider): 
  8.     name = "jiwuspider" 
  9.     allowed_domains = ["wlmq.jiwu.com"
  10.     start_urls = ['http://wlmq.jiwu.com/loupan'
  11.  
  12.     def parse(self, response): 
  13.         ""
  14.         解析每一頁房屋的list 
  15.         :param response:  
  16.         :return:  
  17.         ""
  18.         for url in response.xpath('//a[@class="index_scale"]/@href').extract(): 
  19.             yield Request(url,self.parse_html)  # 取list集合中的url  調(diào)用詳情解析方法 
  20.  
  21.         # 如果下一頁屬性還存在,則把下一頁的url獲取出來 
  22.         nextpage = response.xpath('//a[@class="tg-rownum-next index-icon"]/@href').extract_first() 
  23.         #判斷是否為空 
  24.         if nextpage: 
  25.             yield Request(nextpage,self.parse)  #回調(diào)自己繼續(xù)解析 
  26.  
  27.  
  28.  
  29.     def parse_html(self,response): 
  30.         ""
  31.         解析每一個(gè)房產(chǎn)信息的詳情頁面,生成item 
  32.         :param response:  
  33.         :return:  
  34.         ""
  35.         pattern = re.compile('<script type="text/javascript">.*?lng = \'(.*?)\';.*?lat = \'(.*?)\';.*?bname = \'(.*?)\';.*?' 
  36.                              'address = \'(.*?)\';.*?price = \'(.*?)\';',re.S) 
  37.         item = JiwuItem() 
  38.         results = re.findall(pattern,response.text) 
  39.         for result in results: 
  40.             item['name'] = result[2] 
  41.             item['address'] = result[3] 
  42.             # 對價(jià)格判斷只取數(shù)字,如果為空就設(shè)置為0 
  43.             pricestr =result[4] 
  44.             pattern2 = re.compile('(\d+)'
  45.             s = re.findall(pattern2,pricestr) 
  46.             if len(s) == 0: 
  47.                 item['price'] = 0 
  48.             else:item['price'] = s[0] 
  49.             item['lng'] = result[0] 
  50.             item['lat'] = result[1] 
  51.         yield item 

item.py

  1. # -*- coding: utf-8 -*- 
  2.  
  3. # Define here the models for your scraped items 
  4. # See documentation in
  5. # http://doc.scrapy.org/en/latest/topics/items.html 
  6.  
  7. import scrapy 
  8.  
  9.  
  10. class JiwuItem(scrapy.Item): 
  11.     # define the fields for your item here like
  12.     name = scrapy.Field() 
  13.     price =scrapy.Field() 
  14.     address =scrapy.Field() 
  15.     lng = scrapy.Field() 
  16.     lat = scrapy.Field() 
  17.  
  18.     pass 

pipelines.py 注意此處是吧mongodb的保存方法注釋了,可以自選選擇保存方式 

  1. # -*- coding: utf-8 -*- 
  2.  
  3. # Define your item pipelines here 
  4. # Don't forget to add your pipeline to the ITEM_PIPELINES setting 
  5. # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html 
  6. import pymongo 
  7. from scrapy.conf import settings 
  8. from openpyxl import workbook 
  9.  
  10. class JiwuPipeline(object): 
  11.     wb = workbook.Workbook() 
  12.     ws = wb.active 
  13.     ws.append(['小區(qū)名稱''地址''價(jià)格''經(jīng)度''緯度']) 
  14.     def __init__(self): 
  15.         # 獲取數(shù)據(jù)庫連接信息 
  16.         host = settings['MONGODB_URL'
  17.         port = settings['MONGODB_PORT'
  18.         dbname = settings['MONGODB_DBNAME'
  19.         client = pymongo.MongoClient(host=host, port=port) 
  20.  
  21.         # 定義數(shù)據(jù)庫 
  22.         db = client[dbname] 
  23.         self.table = db[settings['MONGODB_TABLE']] 
  24.  
  25.     def process_item(self, item, spider): 
  26.         jiwu = dict(item) 
  27.         #self.table.insert(jiwu) 
  28.         line = [item['name'], item['address'], str(item['price']), item['lng'], item['lat']] 
  29.         self.ws.append(line) 
  30.         self.wb.save('jiwu.xlsx'
  31.  
  32.         return item 

最后報(bào)表的數(shù)據(jù)

Python爬取房產(chǎn)數(shù)據(jù),在地圖上展現(xiàn)!

mongodb數(shù)據(jù)庫

Python爬取房產(chǎn)數(shù)據(jù),在地圖上展現(xiàn)!

地圖報(bào)表效果圖:https://me.bdp.cn/share/index.html?shareId=sdo_b697418ff7dc4f928bb25e3ac1d52348

責(zé)任編輯:未麗燕 來源: 知乎
相關(guān)推薦

2019-01-02 12:23:30

Python金融數(shù)據(jù)爬取

2016-08-12 09:28:52

APIPythonLinux

2016-12-07 11:18:58

Python爬蟲網(wǎng)站

2017-05-24 15:07:19

Python爬蟲爬取

2021-06-02 15:10:20

PythonScrapy視頻

2017-07-18 09:15:23

Python Craw數(shù)據(jù)爬取

2022-06-15 08:25:07

Python天氣數(shù)據(jù)可視化分析

2020-11-03 14:10:45

Python爬取天氣爬蟲

2018-05-29 21:50:47

爬蟲Python代碼

2021-11-24 10:56:04

特斯拉自動(dòng)駕駛技術(shù)

2021-01-24 16:40:00

Python爬取網(wǎng)站編程語言

2018-01-04 09:20:55

python爬蟲視頻彈幕

2020-12-02 09:42:42

PythonApp抖音視頻

2018-05-31 21:33:05

高德地圖道路積水地圖AI版

2021-06-24 09:41:43

數(shù)據(jù)爬取爬蟲平臺

2020-04-29 11:26:54

Python數(shù)據(jù)消費(fèi)券

2016-07-11 15:55:18

大數(shù)據(jù)

2022-07-12 09:55:34

Selenium爬取數(shù)據(jù)

2011-08-02 12:48:27

iOS開發(fā) 位置

2017-06-23 09:47:42

軟件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號