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

看我如何抓取二手房價(jià)數(shù)據(jù)

開發(fā) 后端
上次為大家介紹了如何通過 Python 抓取新房樓盤價(jià)格信息,很多朋友都在問,那二手房最新的價(jià)格信息要如何抓取呢?好!今天就再來為大家講一講,二手房的房價(jià)信息要怎么抓取。

 上次為大家介紹了如何通過 Python 抓取新房樓盤價(jià)格信息,很多朋友都在問,那二手房最新的價(jià)格信息要如何抓取呢?好!今天就再來為大家講一講,二手房的房價(jià)信息要怎么抓取。

[[324858]]

模塊安裝

同上次新房一樣,這里需要安裝以下模塊(當(dāng)然如果已安裝就不用再裝了):

 

  1. # 安裝引用模塊 
  2. pip3 install bs4 
  3. pip3 install requests 
  4. pip3 install lxml 
  5. pip3 install numpy 
  6. pip3 install pandas 

好了,安裝完成后,就可以開始寫代碼了。至于配置請求頭和代理IP地址的代碼,上次介紹新房已經(jīng)說過了,這里不再贅述,下面直接上抓取代碼。

二手房價(jià)數(shù)據(jù)對象

在這里我們將二手房的房價(jià)信息,創(chuàng)建成一個(gè)對象,后續(xù)我們只要將獲取到的數(shù)據(jù)保存成對象,再處理就會(huì)方便很多。SecHouse 對象代碼如下所示:

 

  1. # 二手房信息對象 
  2. class SecHouse(object): 
  3.     def __init__(self, district, area, name, price, desc, pic): 
  4.         self.district = district 
  5.         self.area = area 
  6.         self.price = price 
  7.         self.name = name 
  8.         self.desc = desc 
  9.         self.pic = pic 
  10.     def text(self): 
  11.         return self.district + "," + \ 
  12.                 self.area + "," + \ 
  13.                 self.name + "," + \ 
  14.                 self.price + "," + \ 
  15.                 self.desc + "," + \ 
  16.                 self.pic 

獲取二手房價(jià)信息并保存

準(zhǔn)備好了,下面我們依然以貝殼為例,批量爬取其北京地區(qū)二手房數(shù)據(jù),并保存到本地。這里我主要想說的是如何抓取數(shù)據(jù)過程,所以這里依然就保存成最簡單的 txt 文本格式。如果想保存到數(shù)據(jù)庫,可以自行修改代碼進(jìn)行保存數(shù)據(jù)庫處理。

獲取區(qū)縣信息

我們在抓取二手房信息時(shí),肯定想知道這個(gè)房源所在地區(qū),所以這里我寫了個(gè)方法把北京市所有區(qū)縣信息抓取下來,并臨時(shí)保存至列表變量里,以備后續(xù)程序中使用,代碼如下:

 

  1. # 獲取區(qū)縣信息 
  2. def get_districts(): 
  3.     # 請求 URL 
  4.     url = 'https://bj.ke.com/xiaoqu/' 
  5.     headers = create_headers() 
  6.     # 請求獲取數(shù)據(jù) 
  7.     response = requests.get(url, timeout=10, headers=headers) 
  8.     html = response.content 
  9.     root = etree.HTML(html) 
  10.     # 處理數(shù)據(jù) 
  11.     elements = root.xpath('///div[3]/div[1]/dl[2]/dd/div/div/a'
  12.     en_names = list() 
  13.     ch_names = list() 
  14.     # 循環(huán)處理對象 
  15.     for element in elements: 
  16.         link = element.attrib['href'
  17.         en_names.append(link.split('/')[-2]) 
  18.         ch_names.append(element.text) 
  19.  
  20.     # 打印區(qū)縣英文和中文名列表 
  21.     for indexname in enumerate(en_names): 
  22.         chinese_city_district_dict[name] = ch_names[index
  23.     return en_names 

獲取地區(qū)板塊

除了上面要獲取區(qū)縣信息,我們還應(yīng)該獲取比區(qū)縣更小的板塊區(qū)域信息,同樣的區(qū)縣內(nèi),不同板塊地區(qū)二手房的價(jià)格等信息肯定不一樣,所以板塊對于我們來說也很重要,具有一次參考價(jià)值。獲取板塊信息代碼如下:

 

  1. # 獲取某個(gè)區(qū)縣下所有板塊信息 
  2. def get_areas(district): 
  3.     # 請求的 URL 
  4.     page = "http://bj.ke.com/xiaoqu/{0}".format(district) 
  5.     # 板塊列表定義 
  6.     areas = list() 
  7.     try: 
  8.         headers = create_headers() 
  9.         response = requests.get(page, timeout=10, headers=headers) 
  10.         html = response.content 
  11.         root = etree.HTML(html) 
  12.         # 獲取標(biāo)簽信息 
  13.         links = root.xpath('//div[3]/div[1]/dl[2]/dd/div/div[2]/a'
  14.  
  15.         # 針對list進(jìn)行處理 
  16.         for link in links: 
  17.             relative_link = link.attrib['href'
  18.             # 最后"/"去掉 
  19.             relative_link = relative_link[:-1] 
  20.             # 獲取最后一節(jié)信息 
  21.             area = relative_link.split("/")[-1] 
  22.             # 去掉區(qū)縣名稱,以防止重復(fù) 
  23.             if area != district: 
  24.                 chinese_area = link.text 
  25.                 chinese_area_dict[area] = chinese_area 
  26.                 # 加入板塊信息列表 
  27.                 areas.append(area) 
  28.         return areas 
  29.     except Exception as e: 
  30.         print(e) 

獲取二手房信息并保存

 

  1. # 創(chuàng)建文件準(zhǔn)備寫入 
  2. with open("sechouse.txt""w", encoding='utf-8'as f: 
  3.     # 定義變量 
  4.     total_page = 1 
  5.     # 初始化 list 
  6.     sec_house_list = list() 
  7.     # 獲取所有區(qū)縣信息 
  8.     districts = get_districts() 
  9.     # 循環(huán)處理區(qū)縣 
  10.     for district in districts: 
  11.         # 獲取某一區(qū)縣下所有板塊信息 
  12.         arealist = get_areas(district) 
  13.         # 循環(huán)遍歷所有板塊下的小區(qū)二手房信息 
  14.         for area in arealist: 
  15.             # 中文區(qū)縣 
  16.             chinese_district = chinese_city_district_dict.get(district, ""
  17.             # 中文版塊 
  18.             chinese_area = chinese_area_dict.get(area, ""
  19.             # 請求地址 
  20.             page = 'http://bj.ke.com/ershoufang/{0}/'.format(area) 
  21.             headers = create_headers() 
  22.             response = requests.get(page, timeout=10, headers=headers) 
  23.             html = response.content 
  24.             # 解析 HTML 
  25.             soup = BeautifulSoup(html, "lxml"
  26.  
  27.             # 獲取總頁數(shù) 
  28.             try: 
  29.                 page_box = soup.find_all('div', class_='page-box')[0] 
  30.                 matches = re.search('.*data-total-count="(\d+)".*', str(page_box)) 
  31.                 # 獲取總頁數(shù) 
  32.                 total_page = int(math.ceil(int(matches.group(1)) / 10)) 
  33.             except Exception as e: 
  34.                 print(e) 
  35.  
  36.             print(total_page) 
  37.             # 設(shè)置請求頭 
  38.             headers = create_headers() 
  39.             # 從第一頁開始,遍歷到最后一頁 
  40.             for i in range(1, total_page + 1): 
  41.                 # 請求地址 
  42.                 page = 'http://bj.ke.com/ershoufang/{0}/pg{1}'.format(area,i) 
  43.                 print(page) 
  44.                 # 獲取返回內(nèi)容 
  45.                 response = requests.get(page, timeout=10, headers=headers) 
  46.                 html = response.content 
  47.                 soup = BeautifulSoup(html, "lxml"
  48.  
  49.                 # 獲得二手房查詢列表 
  50.                 house_elements = soup.find_all('li', class_="clear"
  51.                 # 遍歷每條信息 
  52.                 for house_elem in house_elements: 
  53.                     # 價(jià)格 
  54.                     price = house_elem.find('div', class_="totalPrice"
  55.                     # 標(biāo)題 
  56.                     name = house_elem.find('div', class_='title'
  57.                     # 描述 
  58.                     desc = house_elem.find('div', class_="houseInfo"
  59.                     # 圖片地址 
  60.                     pic = house_elem.find('a', class_="img").find('img', class_="lj-lazy"
  61.  
  62.                     # 清洗數(shù)據(jù) 
  63.                     price = price.text.strip() 
  64.                     name = name.text.replace("\n"""
  65.                     desc = desc.text.replace("\n""").strip() 
  66.                     pic = pic.get('data-original').strip() 
  67.  
  68.                     # 保存二手房對象 
  69.                     sec_house = SecHouse(chinese_district, chinese_area, name, price, desc, pic) 
  70.                     print(sec_house.text()) 
  71.                     sec_house_list.append(sec_house) 
  72.             # 循環(huán)遍歷將信息寫入 txt 
  73.             for sec_house in sec_house_list: 
  74.                 f.write(sec_house.text() + "\n"

到這里代碼就寫好了,現(xiàn)在我們就可以通過命令 python sechouse.py 運(yùn)行代碼進(jìn)行數(shù)據(jù)抓取了。抓取的結(jié)果我們可以打開當(dāng)前目錄下 sechouse.txt 文件查看,結(jié)果如下圖所示:

 

總結(jié)本文為大家介紹了如何通過 Python 將房產(chǎn)網(wǎng)上的二手房數(shù)據(jù)批量抓取下來,經(jīng)過一段時(shí)間的抓取,我們就可以將抓取的結(jié)果進(jìn)行對比分析,看看二手房價(jià)最近是漲還是跌?如果喜歡我們的文章,請關(guān)注收藏再看。

責(zé)任編輯:華軒 來源: Python技術(shù)
相關(guān)推薦

2018-10-22 13:10:43

分析Python房價(jià)

2019-12-19 15:56:10

Python數(shù)據(jù)工具

2018-08-21 21:13:32

爬蟲Python住房

2020-06-05 19:19:03

蘋果促銷商家

2018-10-29 15:41:16

二手硬件處理器

2020-10-27 15:18:39

央行數(shù)字人民幣穆長春

2011-06-08 21:41:14

噴墨打印機(jī)推薦

2013-03-19 14:25:36

2020-03-30 21:36:57

硬件CPU顯卡

2023-07-11 06:32:03

2023-07-12 07:06:23

2022-06-29 09:24:23

顯卡崩盤價(jià)格

2020-06-04 18:30:06

二手硬件CPU主板

2012-08-31 10:02:34

2016-01-29 10:12:01

IT168

2018-12-04 16:44:36

RX比特幣網(wǎng)站

2024-09-26 09:38:06

2024-09-18 15:15:35

2018-01-08 11:24:38

云計(jì)算標(biāo)準(zhǔn)和應(yīng)用大會(huì)轉(zhuǎn)轉(zhuǎn)58同城
點(diǎn)贊
收藏

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