您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)python爬蟲實(shí)戰(zhàn)之爬取房天下新房數(shù)據(jù)的示例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
本示例主要用到requests庫(kù)和bs4庫(kù),requests庫(kù)用來獲取網(wǎng)頁(yè)內(nèi)容,bs4庫(kù)則是解析網(wǎng)頁(yè)內(nèi)容,獲取有用數(shù)據(jù)。
代碼中url可切換當(dāng)?shù)胤刻煜戮W(wǎng)址。
代碼如下
# -*- coding:utf-8 -*- # author:zhoulong ''' 房天下天水新房信息 ''' import requests from bs4 import BeautifulSoup import numpy as np import re URL = 'http://newhouse.tianshui.fang.com/house/s/b91/' HTML = requests.get(URL) SOUP = BeautifulSoup(HTML.content, 'html.parser', from_encoding='gb18030') last_page = SOUP.select('.last') page_number = int(last_page[0]['href'].split('/')[3].split('9')[1])#根據(jù)尾頁(yè)劃分頁(yè)碼 url_demo = 'http://newhouse.tianshui.fang.com/house/s/b9{}/'#i+1,name.text.strip(), #房?jī)r(jià)價(jià)格 house_price_list=[] for i in range(1,(page_number+1)): url = url_demo.format(i) html = requests.get(url) soup = BeautifulSoup(html.content,'html.parser',from_encoding='gb18030') names = soup.select('.nlcd_name a')#class定位組合查找 adresses = soup.select('.address a')#查找地址 all_type = soup.findAll(name="span", attrs={"class": re.compile(r"forSale|inSale|outSale|zusale|zushou")})#出售 all_money = soup.findAll(name="div", attrs={"class": re.compile(r"nhouse_price|kanesf")})#價(jià)格 for i,name in enumerate(names): print(i+1,' name:'+name.text.strip(),' address:'+''.join(re.split(r'\s+', adresses[i].text.replace('\n','').replace('',''))), all_type[i].text,' house_price: '+all_money[i].text.replace('\n','')) house_price_list.append(re.findall('\d+',all_money[i].text.replace('\n',''))) house_price_list=[int(i[0]) for i in house_price_list if i] print('*'*80) print('* '+' 房?jī)r(jià)均價(jià):'+str(np.mean(house_price_list))+' '*60+'*') print('* '+' 房?jī)r(jià)最高價(jià):'+str(np.max(house_price_list))+' '*60+'*') print('* '+' 房?jī)r(jià)最低價(jià):'+str(np.min(house_price_list))+' '*61+'*') print('*'*80)
執(zhí)行結(jié)果
關(guān)于python爬蟲實(shí)戰(zhàn)之爬取房天下新房數(shù)據(jù)的示例就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。