基于Python實現(xiàn)最新房價信息的獲取
整個數(shù)據(jù)獲取的信息是通過房源平臺獲取的,通過下載網(wǎng)頁元素并進(jìn)行數(shù)據(jù)提取分析完成整個過程

導(dǎo)入相關(guān)的網(wǎng)頁下載、數(shù)據(jù)解析、數(shù)據(jù)處理庫
from fake_useragent import UserAgent # 身份信息生成庫 from bs4 import BeautifulSoup # 網(wǎng)頁元素解析庫 import numpy as np # 科學(xué)計算庫 import requests # 網(wǎng)頁下載庫 from requests.exceptions import RequestException # 網(wǎng)絡(luò)請求異常庫 import pandas as pd # 數(shù)據(jù)處理庫
然后,在開始之前初始化一個身份信息生成的對象,用于后面隨機(jī)生成網(wǎng)頁下載時的身份信息。
user_agent = UserAgent()
編寫一個網(wǎng)頁下載函數(shù)get_html_txt,從相應(yīng)的url地址下載網(wǎng)頁的html文本。
def get_html_txt(url, page_index):
'''
獲取網(wǎng)頁html文本信息
:param url: 爬取地址
:param page_index:當(dāng)前頁數(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}頁網(wǎng)頁元素失??!'.format(page_index))
return ''
編寫網(wǎng)頁元素處理函數(shù)catch_html_data,用于解析網(wǎng)頁元素,并將解析后的數(shù)據(jù)元素保存到csv文件中。
def catch_html_data(url, page_index):
'''
處理網(wǎng)頁元素數(shù)據(jù)
:param url: 爬蟲地址
:param page_index:
:return:
'''
# 下載網(wǎng)頁元素
html_txt = str(get_html_txt(url, page_index))
if html_txt.strip() != '':
# 初始化網(wǎng)頁元素對象
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))
# 提取房源單價信息
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))
# 提取房源總價信息
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)', '房源均價', '房源總價'])
h_info.to_csv('北京房源信息.csv', mode='a+', index=False, header=False)
print('第{0}頁房源信息數(shù)據(jù)存儲成功!'.format(page_index))
else:
print('網(wǎng)頁元素解析失敗!')
編寫多線程處理函數(shù),初始化網(wǎng)絡(luò)網(wǎng)頁下載地址,并使用多線程啟動調(diào)用業(yè)務(wù)處理函數(shù)catch_html_data,啟動線程完成整個業(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ù)存儲結(jié)果展示效果

以上就是基于Python實現(xiàn)最新房價信息的獲取的詳細(xì)內(nèi)容,更多關(guān)于Python獲取房價信息的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
瀏覽器常用基本操作之python3+selenium4自動化測試(基礎(chǔ)篇3)
瀏覽器常用基本操作有很多種,今天給大家介紹python3+selenium4自動化測試的操作方法,是最最基礎(chǔ)的一篇,對python3 selenium4自動化測試相關(guān)知識感興趣的朋友一起看看吧2021-05-05
Python3實現(xiàn)的判斷環(huán)形鏈表算法示例
這篇文章主要介紹了Python3實現(xiàn)的判斷環(huán)形鏈表算法,涉及Python針對環(huán)形鏈表的遍歷、判斷相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
python GUI庫圖形界面開發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實例,需要的朋友可以參考下2020-03-03
Python3.9用pip安裝wordcloud庫失敗的解決過程
一般在命令行輸入pip install wordcloud 總會顯示安裝失敗,所以下面這篇文章主要給大家介紹了關(guān)于Python3.9用pip安裝wordcloud庫失敗的解決過程,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

