欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python實現(xiàn)最新房價信息的獲取

 更新時間:2022年04月18日 08:20:42   作者:Python?集中營  
這篇文章主要為大家介紹了如何利用Python獲取房價信息(以北京為例),整個數(shù)據(jù)獲取的信息是通過房源平臺獲取的,通過下載網(wǎng)頁元素并進(jìn)行數(shù)據(jù)提取分析完成整個過程,需要的可以參考一下

整個數(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)文章

  • 用Python寫腳本自動評論再也不怕碰到噴子

    用Python寫腳本自動評論再也不怕碰到噴子

    這篇文章主要介紹了如何用Python寫腳本哎實現(xiàn)網(wǎng)站上自動評論,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • 瀏覽器常用基本操作之python3+selenium4自動化測試(基礎(chǔ)篇3)

    瀏覽器常用基本操作之python3+selenium4自動化測試(基礎(chǔ)篇3)

    瀏覽器常用基本操作有很多種,今天給大家介紹python3+selenium4自動化測試的操作方法,是最最基礎(chǔ)的一篇,對python3 selenium4自動化測試相關(guān)知識感興趣的朋友一起看看吧
    2021-05-05
  • Python爬蟲PyQuery庫基本用法入門教程

    Python爬蟲PyQuery庫基本用法入門教程

    這篇文章主要介紹了Python爬蟲PyQuery庫基本用法,結(jié)合實例形式較為詳細(xì)的分析了pyQuery庫字符串初始化、打開網(wǎng)頁、css屬性、標(biāo)簽內(nèi)容等獲取、DOM基本操作等相關(guān)技巧與使用注意事項,需要的朋友可以參考下
    2018-08-08
  • 淺談Python基礎(chǔ)—判斷和循環(huán)

    淺談Python基礎(chǔ)—判斷和循環(huán)

    這篇文章主要介紹了Python基礎(chǔ)—判斷和循環(huán),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Python3實現(xiàn)的判斷環(huán)形鏈表算法示例

    Python3實現(xiàn)的判斷環(huán)形鏈表算法示例

    這篇文章主要介紹了Python3實現(xiàn)的判斷環(huán)形鏈表算法,涉及Python針對環(huán)形鏈表的遍歷、判斷相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • 在SAE上部署Python的Django框架的一些問題匯總

    在SAE上部署Python的Django框架的一些問題匯總

    這篇文章主要介紹了在SAE上部署Python的Django框架的一些問題匯總,SAE是新浪的一個在線APP部署平臺,并且對Python應(yīng)用提供相關(guān)支持,需要的朋友可以參考下
    2015-05-05
  • Python入門篇之列表和元組

    Python入門篇之列表和元組

    Python包含6種內(nèi)建序列:列表、元組、字符串、Unicode字符串、buffer對象、xrange對象。本篇主要討論最常用的兩種類型:列表、元組
    2014-10-10
  • python GUI庫圖形界面開發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實例

    python GUI庫圖形界面開發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實例

    這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實例,需要的朋友可以參考下
    2020-03-03
  • python創(chuàng)建生成器以及訪問的方法詳解

    python創(chuàng)建生成器以及訪問的方法詳解

    這篇文章主要介紹了python創(chuàng)建生成器以及訪問的方法詳解,與列表一次性地將數(shù)據(jù)全都加載到內(nèi)存不同的是,生成器使用推斷加載數(shù)據(jù),每次只推斷出一個對象,在數(shù)據(jù)量比較大時,可以節(jié)省內(nèi)存,需要的朋友可以參考下
    2023-11-11
  • Python3.9用pip安裝wordcloud庫失敗的解決過程

    Python3.9用pip安裝wordcloud庫失敗的解決過程

    一般在命令行輸入pip install wordcloud 總會顯示安裝失敗,所以下面這篇文章主要給大家介紹了關(guān)于Python3.9用pip安裝wordcloud庫失敗的解決過程,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06

最新評論