基于Python實(shí)現(xiàn)最新房?jī)r(jià)信息的獲取
整個(gè)數(shù)據(jù)獲取的信息是通過(guò)房源平臺(tái)獲取的,通過(guò)下載網(wǎng)頁(yè)元素并進(jìn)行數(shù)據(jù)提取分析完成整個(gè)過(guò)程
導(dǎo)入相關(guān)的網(wǎng)頁(yè)下載、數(shù)據(jù)解析、數(shù)據(jù)處理庫(kù)
from fake_useragent import UserAgent # 身份信息生成庫(kù) from bs4 import BeautifulSoup # 網(wǎng)頁(yè)元素解析庫(kù) import numpy as np # 科學(xué)計(jì)算庫(kù) import requests # 網(wǎng)頁(yè)下載庫(kù) from requests.exceptions import RequestException # 網(wǎng)絡(luò)請(qǐng)求異常庫(kù) import pandas as pd # 數(shù)據(jù)處理庫(kù)
然后,在開(kāi)始之前初始化一個(gè)身份信息生成的對(duì)象,用于后面隨機(jī)生成網(wǎng)頁(yè)下載時(shí)的身份信息。
user_agent = UserAgent()
編寫(xiě)一個(gè)網(wǎng)頁(yè)下載函數(shù)get_html_txt,從相應(yīng)的url地址下載網(wǎng)頁(yè)的html文本。
def get_html_txt(url, page_index): ''' 獲取網(wǎng)頁(yè)html文本信息 :param url: 爬取地址 :param page_index:當(dāng)前頁(yè)數(shù) :return: ''' try: headers = { 'user-agent': user_agent.random } response = requests.request("GET", url, headers=headers, timeout=10) html_txt = response.text return html_txt except RequestException as e: print('獲取第{0}頁(yè)網(wǎng)頁(yè)元素失敗!'.format(page_index)) return ''
編寫(xiě)網(wǎng)頁(yè)元素處理函數(shù)catch_html_data,用于解析網(wǎng)頁(yè)元素,并將解析后的數(shù)據(jù)元素保存到csv文件中。
def catch_html_data(url, page_index): ''' 處理網(wǎng)頁(yè)元素?cái)?shù)據(jù) :param url: 爬蟲(chóng)地址 :param page_index: :return: ''' # 下載網(wǎng)頁(yè)元素 html_txt = str(get_html_txt(url, page_index)) if html_txt.strip() != '': # 初始化網(wǎng)頁(yè)元素對(duì)象 beautifulSoup = BeautifulSoup(html_txt, 'lxml') # 解析房源列表 h_list = beautifulSoup.select('.resblock-list-wrapper li') # 遍歷當(dāng)前房源的詳細(xì)信息 for n in range(len(h_list)): h_detail = h_list[n] # 提取房源名稱 h_detail_name = h_detail.select('.resblock-name a.name') h_detail_name = [m.get_text() for m in h_detail_name] h_detail_name = ' '.join(map(str, h_detail_name)) # 提取房源類型 h_detail_type = h_detail.select('.resblock-name span.resblock-type') h_detail_type = [m.get_text() for m in h_detail_type] h_detail_type = ' '.join(map(str, h_detail_type)) # 提取房源銷售狀態(tài) h_detail_status = h_detail.select('.resblock-name span.sale-status') h_detail_status = [m.get_text() for m in h_detail_status] h_detail_status = ' '.join(map(str, h_detail_status)) # 提取房源單價(jià)信息 h_detail_price = h_detail.select('.resblock-price .main-price .number') h_detail_price = [m.get_text() for m in h_detail_price] h_detail_price = ' '.join(map(str, h_detail_price)) # 提取房源總價(jià)信息 h_detail_total_price = h_detail.select('.resblock-price .second') h_detail_total_price = [m.get_text() for m in h_detail_total_price] h_detail_total_price = ' '.join(map(str, h_detail_total_price)) h_info = [h_detail_name, h_detail_type, h_detail_status, h_detail_price, h_detail_total_price] h_info = np.array(h_info) h_info = h_info.reshape(-1, 5) h_info = pd.DataFrame(h_info, columns=['房源名稱', '房源類型', '房源狀態(tài)', '房源均價(jià)', '房源總價(jià)']) h_info.to_csv('北京房源信息.csv', mode='a+', index=False, header=False) print('第{0}頁(yè)房源信息數(shù)據(jù)存儲(chǔ)成功!'.format(page_index)) else: print('網(wǎng)頁(yè)元素解析失敗!')
編寫(xiě)多線程處理函數(shù),初始化網(wǎng)絡(luò)網(wǎng)頁(yè)下載地址,并使用多線程啟動(dòng)調(diào)用業(yè)務(wù)處理函數(shù)catch_html_data,啟動(dòng)線程完成整個(gè)業(yè)務(wù)流程。
import threading # 導(dǎo)入線程處理模塊 def thread_catch(): ''' 線程處理函數(shù) :return: ''' for num in range(1, 50, 3): url_pre = "https://bj.fang.lianjia.com/loupan/pg{0}/".format(str(num)) url_cur = "https://bj.fang.lianjia.com/loupan/pg{0}/".format(str(num + 1)) url_aft = "https://bj.fang.lianjia.com/loupan/pg{0}/".format(str(num + 2)) thread_pre = threading.Thread(target=catch_html_data, args=(url_pre, num)) thread_cur = threading.Thread(target=catch_html_data, args=(url_cur, num + 1)) thread_aft = threading.Thread(target=catch_html_data, args=(url_aft, num + 2)) thread_pre.start() thread_cur.start() thread_aft.start() thread_catch()
數(shù)據(jù)存儲(chǔ)結(jié)果展示效果
以上就是基于Python實(shí)現(xiàn)最新房?jī)r(jià)信息的獲取的詳細(xì)內(nèi)容,更多關(guān)于Python獲取房?jī)r(jià)信息的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用Python寫(xiě)腳本自動(dòng)評(píng)論再也不怕碰到噴子
這篇文章主要介紹了如何用Python寫(xiě)腳本哎實(shí)現(xiàn)網(wǎng)站上自動(dòng)評(píng)論,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08瀏覽器常用基本操作之python3+selenium4自動(dòng)化測(cè)試(基礎(chǔ)篇3)
瀏覽器常用基本操作有很多種,今天給大家介紹python3+selenium4自動(dòng)化測(cè)試的操作方法,是最最基礎(chǔ)的一篇,對(duì)python3 selenium4自動(dòng)化測(cè)試相關(guān)知識(shí)感興趣的朋友一起看看吧2021-05-05Python爬蟲(chóng)PyQuery庫(kù)基本用法入門(mén)教程
這篇文章主要介紹了Python爬蟲(chóng)PyQuery庫(kù)基本用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了pyQuery庫(kù)字符串初始化、打開(kāi)網(wǎng)頁(yè)、css屬性、標(biāo)簽內(nèi)容等獲取、DOM基本操作等相關(guān)技巧與使用注意事項(xiàng),需要的朋友可以參考下2018-08-08Python3實(shí)現(xiàn)的判斷環(huán)形鏈表算法示例
這篇文章主要介紹了Python3實(shí)現(xiàn)的判斷環(huán)形鏈表算法,涉及Python針對(duì)環(huán)形鏈表的遍歷、判斷相關(guān)操作技巧,需要的朋友可以參考下2019-03-03在SAE上部署Python的Django框架的一些問(wèn)題匯總
這篇文章主要介紹了在SAE上部署Python的Django框架的一些問(wèn)題匯總,SAE是新浪的一個(gè)在線APP部署平臺(tái),并且對(duì)Python應(yīng)用提供相關(guān)支持,需要的朋友可以參考下2015-05-05python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-03-03python創(chuàng)建生成器以及訪問(wèn)的方法詳解
這篇文章主要介紹了python創(chuàng)建生成器以及訪問(wèn)的方法詳解,與列表一次性地將數(shù)據(jù)全都加載到內(nèi)存不同的是,生成器使用推斷加載數(shù)據(jù),每次只推斷出一個(gè)對(duì)象,在數(shù)據(jù)量比較大時(shí),可以節(jié)省內(nèi)存,需要的朋友可以參考下2023-11-11Python3.9用pip安裝wordcloud庫(kù)失敗的解決過(guò)程
一般在命令行輸入pip install wordcloud 總會(huì)顯示安裝失敗,所以下面這篇文章主要給大家介紹了關(guān)于Python3.9用pip安裝wordcloud庫(kù)失敗的解決過(guò)程,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06