您好,登錄后才能下訂單哦!
這篇文章主要介紹了python爬蟲如何爬取租房信息在地圖上顯示,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
python爬蟲用了比較簡單的urllib.parse和requests,把爬來的數據顯示在地圖上。接下里我們話不多說直接上代碼:
1.安裝python環境和編輯器(自行度娘)
2.本人以58品牌公寓為例,爬取在杭州地區價格在2000-4000的公寓。
#-*- coding:utf-8 -*- from bs4 import BeautifulSoup from urllib.parse import urljoin import requests import csv import time
以上是需要引入的模塊
url = "http://hz.58.com/pinpaigongyu/pn/{page}/?minprice=2000_4000" #已完成的頁數序號,初時為0 page = 0
以上的全局變量
csv_file = open(r"c:\users\****\Desktop\houoseNew.csv","a+",newline='') csv_writer = csv.writer(csv_file, delimiter=',')
自定義某個位置來保存爬取得數據,本人把爬取得數據保存為csv格式便于編輯(其中”a+”表示可以多次累加編輯在后面插入數據,建議不要使用“wb”哦!newline=”表示沒有隔行)
while True: #為了防止網站屏蔽ip,設置了時間定時器每隔5秒爬一下。打完一局農藥差不多都爬取過來了。 time.sleep(5) page +=1 #替換URL中page變量 print (url.format(page=page)+"ok") response = requests.get(url.format(page=page)) html=BeautifulSoup(response.text) #尋找html中DOM節點li house_list = html.select(".list > li") # 循環在讀不到新的房源時結束 if not house_list: break for house in house_list: #根據hml的DOM節點獲取自己需要的數據 house_title = house.select("h3")[0].string house_url = urljoin(url, house.select("a")[0]["href"]) house_pic = urljoin(url, house.select("img")[0]["lazy_src"]) house_info_list = house_title.split() # 如果第一列是公寓名 則取第二列作為地址 if "公寓" in house_info_list[0] or "青年社區" in house_info_list[0]: house_location = house_info_list[0] else: house_location = house_info_list[1] house_money = house.select(".money")[0].select("b")[0].string csv_writer.writerow([house_title, house_location, house_money,house_pic ,house_url]) #最后不要忘記關閉節流 csv_file.close()
如果網站屏蔽了你的ip,你可以做一個ip地址數組放在http的頭部具體度娘一下吧。
接下來我們寫html
只是簡單的寫了一下寫的不好見諒。用的是高德地圖,具體的js api可以到高德開發者上去看。
<body> <div id="container"></div> <div class="control-panel"> <div class="control-entry"> <label>選擇工作地點:</label> <div class="control-input"> <input id="work-location" type="text"> </div> </div> <div class="control-entry"> <label>選擇通勤方式:</label> <div class="control-input"> <input type="radio" name="vehicle" value="SUBWAY,BUS" onClick="takeBus(this)" checked/> 公交+地鐵 <input type="radio" name="vehicle" value="SUBWAY" onClick="takeSubway(this)"/> 地鐵 <input type="radio" name="vehicle" value="WALK" onClick="takeWalk(this)"/> 走路 <input type="radio" name="vehicle" value="BIKE" onClick="takeBike(this)"/> 騎車 </div> </div> <div class="control-entry"> <label>導入房源文件:</label> <div class="control-input"> <input type="file" name="file" id="fileCsv"/> <button onclick="changeCsv()">開始</button> </div> </div> </div> <div id="transfer-panel"></div> <script> var map = new AMap.Map("container", { resizeEnable: true, zoomEnable: true, center: [120.1256856402492, 30.27289264553506], zoom: 12 }); //添加標尺 var scale = new AMap.Scale(); map.addControl(scale); //公交到達圈對象 var arrivalRange = new AMap.ArrivalRange(); //經度,緯度,時間(用不到),通勤方式(默認是地鐵+公交+走路+騎車) var x, y, t, vehicle = "SUBWAY,BUS"; //工作地點,工作標記 var workAddress, workMarker; //房源標記隊列 var rentMarkerArray = []; //多邊形隊列,存儲公交到達的計算結果 var polygonArray = []; //路徑規劃 var amapTransfer; //信息窗體對象 var infoWindow = new AMap.InfoWindow({ offset: new AMap.Pixel(0, -30) }); //地址補完的使用 var auto = new AMap.Autocomplete({ //通過id指定輸入元素 input: "work-location" }); //添加事件監聽,在選擇補完的地址后調用workLocationSelected AMap.event.addListener(auto, "select", workLocationSelected); function takeBus(radio) { vehicle = radio.value; loadWorkLocation() } function takeSubway(radio) { vehicle = radio.value; loadWorkLocation() } function takeWalk(radio){ vehicle = radio.value; loadWorkLocation() } function takeBike(radio) { vehicle = radio.value; loadWorkLocation() } //獲取加載的文件 function changeCsv() { $("#fileCsv").csv2arr(function (res) { $.each(res, function (k, p) { if (res[k][1]) { //addMarkerByAddress(地址,價格,展示的圖片) addMarkerByAddress(res[k][1], res[k][2],res[k][3]) } }) }); } function workLocationSelected(e) { workAddress = e.poi.name; loadWorkLocation(); } function loadWorkMarker(x, y, locationName) { workMarker = new AMap.Marker({ map: map, title: locationName, icon: 'http://webapi.amap.com/theme/v1.3/markers/n/mark_r.png', position: [x, y] }); } function loadWorkRange(x, y, t, color, v) { arrivalRange.search([x, y], t, function (status, result) { if (result.bounds) { for (var i = 0; i < result.bounds.length; i++) { //新建多邊形對象 var polygon = new AMap.Polygon({ map: map, fillColor: color, fillOpacity: "0.4", strokeColor: color, strokeOpacity: "0.8", strokeWeight: 1 }); //得到到達圈的多邊形路徑 polygon.setPath(result.bounds[i]); polygonArray.push(polygon); } } }, { policy: v }); } function addMarkerByAddress(address, money,imgUrl) { var geocoder = new AMap.Geocoder({ city: "杭州", radius: 1000 }); geocoder.getLocation(address, function (status, result) { var iconValue = ""; var _money=money; if (money.indexOf("-") > -1) { _money = money.split("-")[1]; } //如果價格高于3000元/月在地圖上顯示紅色,低于的話顯示藍色 if (parseFloat(_money) > 3000) { iconValue="http://webapi.amap.com/theme/v1.3/markers/n/mark_r.png"; }else{ iconValue = "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png"; } if (status === "complete" && result.info === 'OK') { var geocode = result.geocodes[0]; rentMarker = new AMap.Marker({ map: map, title: address, icon:iconValue, animation:"AMAP_ANIMATION_DROP", position: [geocode.location.getLng(), geocode.location.getLat()] }) ; rentMarkerArray.push(rentMarker); //鼠標點擊標記顯示相應的內容 rentMarker.content = "<img src='"+imgUrl+"'/><div>房源:<a target = '_blank' href='http://bj.58.com/pinpaigongyu/?key=" + address + "'>" + address + "</a><p>價格:"+money+"</p><div>" rentMarker.on('click', function (e) { infoWindow.setContent(e.target.content); infoWindow.open(map, e.target.getPosition()); if (amapTransfer) amapTransfer.clear(); amapTransfer = new AMap.Transfer({ map: map, policy: AMap.TransferPolicy.LEAST_TIME, city: "杭州市", panel: 'transfer-panel' }); amapTransfer.search([{ keyword: workAddress }, { keyword: address }], function (status, result) { }) }); } }) } function delWorkLocation() { if (polygonArray) map.remove(polygonArray); if (workMarker) map.remove(workMarker); polygonArray = []; } function delRentLocation() { if (rentMarkerArray) map.remove(rentMarkerArray); rentMarkerArray = []; } function loadWorkLocation() { //首先清空地圖上已有的到達圈 delWorkLocation(); var geocoder = new AMap.Geocoder({ city: "杭州", radius: 1000 }); geocoder.getLocation(workAddress, function (status, result) { if (status === "complete" && result.info === 'OK') { var geocode = result.geocodes[0]; x = geocode.location.getLng(); y = geocode.location.getLat(); //加載工作地點標記 loadWorkMarker(x, y); //加載60分鐘內工作地點到達圈 loadWorkRange(x, y, 60, "#3f67a5", vehicle); //地圖移動到工作地點的位置 map.setZoomAndCenter(12, [x, y]); } }) } </script> </body>
想要獲取完整的代碼github:https://github.com/DIVIBEAR/pythonDemo.git
新手上路,老司機們勿噴!
感謝你能夠認真閱讀完這篇文章,希望小編分享的“python爬蟲如何爬取租房信息在地圖上顯示”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。