基于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()
編寫一個(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 ''
編寫網(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è)元素解析失??!')
編寫多線程處理函數(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 Opencv中用compareHist函數(shù)進(jìn)行直方圖比較對(duì)比圖片
這篇文章主要介紹了Python Opencv中用compareHist函數(shù)進(jìn)行直方圖比較進(jìn)行對(duì)比圖片,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python實(shí)現(xiàn)圖數(shù)據(jù)處理的完整指南
圖是一種非常重要的數(shù)據(jù)結(jié)構(gòu),在Python中,我們可以使用鄰接矩陣來(lái)表示圖,這篇文章主要為大家介紹了Python實(shí)現(xiàn)圖數(shù)據(jù)處理的相關(guān)知識(shí),需要的可以參考下2024-04-04
解決Python 寫文件報(bào)錯(cuò)TypeError的問(wèn)題
這篇文章主要介紹了解決Python 寫文件報(bào)錯(cuò)TypeError的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
python使用PyCharm進(jìn)行遠(yuǎn)程開(kāi)發(fā)和調(diào)試
這篇文章主要介紹了python使用PyCharm進(jìn)行遠(yuǎn)程開(kāi)發(fā)和調(diào)試,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Python使用Flask編寫一個(gè)網(wǎng)站的代碼指南
使用Flask編寫一個(gè)網(wǎng)站是一個(gè)相對(duì)簡(jiǎn)單且有趣的過(guò)程,Flask是一個(gè)用Python編寫的輕量級(jí)Web應(yīng)用框架,它易于上手,同時(shí)也非常強(qiáng)大,適合構(gòu)建從簡(jiǎn)單的博客到復(fù)雜的Web應(yīng)用的各種項(xiàng)目,以下是一個(gè)使用Flask編寫簡(jiǎn)單網(wǎng)站的指南,包括代碼示例,需要的朋友可以參考下2024-11-11
利用Python操作MongoDB數(shù)據(jù)庫(kù)的詳細(xì)指南
MongoDB是由C++語(yǔ)言編寫的非關(guān)系型數(shù)據(jù)庫(kù),是一個(gè)基于分布式文件存儲(chǔ)的開(kāi)源數(shù)據(jù)庫(kù)系統(tǒng),其內(nèi)容存儲(chǔ)形式類似JSON對(duì)象,下面這篇文章主要給大家介紹了關(guān)于利用Python操作MongoDB數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2023-02-02

