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

Python多線程采集二手房源數(shù)據(jù)信息流程詳解

 更新時(shí)間:2023年05月17日 11:13:13   作者:魔王不會(huì)哭  
這篇文章主要介紹了Python多線程采集二手房源數(shù)據(jù)信息流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧

環(huán)境使用

  • Python 3.8
  • Pycharm

模塊使用

  • requests >>> pip install requests 數(shù)據(jù)請(qǐng)求模塊
  • parsel >>> pip install parsel 數(shù)據(jù)解析模塊
  • csv 內(nèi)置模塊

代碼展示

導(dǎo)入模塊

# 導(dǎo)入數(shù)據(jù)請(qǐng)求模塊 --> 第三方模塊, 需要安裝 pip install requests
import requests
# 導(dǎo)入解析數(shù)據(jù)模塊 --> 第三方模塊, 需要安裝 pip install parsel
import parsel
# 導(dǎo)入csv模塊
import csv

創(chuàng)建文件

f = open('data.csv', mode='w', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
    '標(biāo)題',
    '小區(qū)',
    '區(qū)域',
    '售價(jià)',
    '單價(jià)',
    '戶型',
    '面積',
    '朝向',
    '裝修',
    '樓層',
    '年份',
    '建筑類(lèi)型',
    '詳情頁(yè)',
])
csv_writer.writeheader()

“”"

發(fā)送請(qǐng)求, 模擬瀏覽器 對(duì)于 url地址 發(fā)送請(qǐng)求

模擬瀏覽器 < 請(qǐng)求頭 headers >

“”"

模擬瀏覽器

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}

請(qǐng)求網(wǎng)址/網(wǎng)站

url = 'https://*****/ershoufang/'

發(fā)送請(qǐng)求

response = requests.get(url=url, headers=headers)
# <Response [200]> 響應(yīng)對(duì)象 200 狀態(tài)碼 表示請(qǐng)求成功
print(response)

獲取數(shù)據(jù), 獲取網(wǎng)頁(yè)源代碼 <獲取服務(wù)器返回響應(yīng)數(shù)據(jù)>

解析數(shù)據(jù), 提取我們想要的數(shù)據(jù)內(nèi)容

解析方法:

  • re: 對(duì)于字符串?dāng)?shù)據(jù)直接進(jìn)行解析提取
  • css: 根據(jù)標(biāo)簽屬性提取數(shù)據(jù)內(nèi)容
  • xpath: 根據(jù)標(biāo)簽節(jié)點(diǎn)提取數(shù)據(jù)內(nèi)容

使用css: 根據(jù)標(biāo)簽屬性提取數(shù)據(jù)內(nèi)容

把獲取到html字符串?dāng)?shù)據(jù), 轉(zhuǎn)成可解析對(duì)象

selector = parsel.Selector(response.text)

獲取所有房源信息所在li標(biāo)簽

lis = selector.css('.sellListContent li.clear')

for循環(huán)遍歷

for li in lis:
    """
    提取具體房源信息: 標(biāo)題 / 價(jià)格 / 位置 / 戶型...
    .title a --> 表示定位class類(lèi)名為title下面a標(biāo)簽
    """
    title = li.css('.title a::text').get()  # 標(biāo)題
    info_list = li.css('.positionInfo a::text').getall()
    area = info_list[0]  # 小區(qū)名字
    area_1 = info_list[1]  # 地區(qū)
    totalPrice = li.css('.totalPrice span::text').get()  # 售價(jià)
    unitPrice = li.css('.unitPrice span::text').get().replace('元/平', '').replace(',', '')  # 單價(jià)
    houseInfo = li.css('.houseInfo::text').get().split(' | ')  # 信息
    houseType = houseInfo[0]  # 戶型
    houseArea = houseInfo[1].replace('平米', '')  # 面積
    houseFace = houseInfo[2]  # 朝向
    fitment = houseInfo[3]  # 裝修
    fool = houseInfo[4]  # 樓層
    if len(houseInfo) == 7 and '年' in houseInfo[5]:
        year = houseInfo[5].replace('年建', '')
    else:
        year = ''
    house = houseInfo[-1]  # 建筑類(lèi)型
    href = li.css('.title a::attr(href)').get()  # 詳情頁(yè)
    dit = {
        '標(biāo)題': title,
        '小區(qū)': area,
        '區(qū)域': area_1,
        '售價(jià)': totalPrice,
        '單價(jià)': unitPrice,
        '戶型': houseType,
        '面積': houseArea,
        '朝向': houseFace,
        '裝修': fitment,
        '樓層': fool,
        '年份': year,
        '建筑類(lèi)型': house,
        '詳情頁(yè)': href,
    }
    csv_writer.writerow(dit)
    print(dit)
    # print(title, area, area_1, totalPrice, unitPrice, houseType, houseArea, houseFace, fitment, fool, year, house, href)

多線程

導(dǎo)入模塊

import requests
import parsel
import re
import csv
# 線程池模塊
import concurrent.futures
import time

發(fā)送請(qǐng)求函數(shù)

def get_response(html_url):
    """
    發(fā)送請(qǐng)求函數(shù)
    :param html_url:
    :return:
    """
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    return response

獲取數(shù)據(jù)函數(shù)

def get_content(html_url):
    """
    :param html_url:
    :return:
    """
    response = get_response(html_url)
    html_data = get_response(link).text
    selector = parsel.Selector(response.text)
    select = parsel.Selector(html_data)
    lis = selector.css('.sellListContent li')
    content_list = []
    for li in lis:
        title = li.css('.title a::text').get()  # 標(biāo)題
        area = '-'.join(li.css('.positionInfo a::text').getall())  # 小區(qū)
        Price = li.css('.totalPrice span::text').get()  # 總價(jià)
        Price_1 = li.css('.unitPrice span::text').get().replace('元/平', '')  # 單價(jià)
        houseInfo = li.css('.houseInfo::text').get()  # 信息
        HouseType = houseInfo.split(' | ')[0]  # 戶型
        HouseArea = houseInfo.split(' | ')[1].replace('平米', '')  # 面積
        direction = houseInfo.split(' | ')[2].replace(' ', '')  # 朝向
        renovation = houseInfo.split(' | ')[3]  # 裝修
        floor_info = houseInfo.split(' | ')[4]
        floor = floor_info[:3]  # 樓層
        floor_num = re.findall('(\d+)層', floor_info)[0]  # 層數(shù)
        BuildingType = houseInfo.split(' | ')[-1]
        string = select.css('.comments div:nth-child(7) .comment_text::text').get()
        href = li.css('.title a::attr(href)').get()  # 詳情頁(yè)
        if len(houseInfo.split(' | ')) == 6:
            date = 'None'
        else:
            date = houseInfo.split(' | ')[5].replace('年建', '')  # 日期
        print(string)
        dit = {
            '標(biāo)題': title,
            '內(nèi)容': string,
            '小區(qū)': area,
            '總價(jià)': Price,
            '單價(jià)': Price_1,
            '戶型': HouseType,
            '面積': HouseArea,
            '朝向': direction,
            '裝修': renovation,
            '樓層': floor,
            '層數(shù)': floor_num,
            '建筑日期': date,
            '建筑類(lèi)型': BuildingType,
            '詳情頁(yè)': href,
        }
        content_list.append(dit)
    return content_list

主函數(shù)

def main(page):
    """
    :param page:
    :return:
    """
    print(f'===============正在采集第{page}頁(yè)的數(shù)據(jù)內(nèi)容===============')
    url = f'https:///ershoufang/yuelu/p{page}/'
    content_list = get_content(html_url=url)
    for content in content_list:
        csv_writer.writerow(content)
if __name__ == '__main__':
    time_1 = time.time()
    link = 'http://******/article/149'
    # 創(chuàng)建文件
    f = open('data多線程.csv', mode='a', encoding='utf-8', newline='')
    csv_writer = csv.DictWriter(f, fieldnames=[
        '標(biāo)題',
        '內(nèi)容',
        '小區(qū)',
        '總價(jià)',
        '單價(jià)',
        '戶型',
        '面積',
        '朝向',
        '裝修',
        '樓層',
        '層數(shù)',
        '建筑日期',
        '建筑類(lèi)型',
        '詳情頁(yè)',
    ])
    csv_writer.writeheader()
    # 線程池執(zhí)行器 max_workers 最大線程數(shù)
    exe = concurrent.futures.ThreadPoolExecutor(max_workers=10)
    for page in range(1, 11):
        exe.submit(main, page)
    exe.shutdown()
    time_2 = time.time()
    use_time = int(time_2 - time_1)
    # 總計(jì)耗時(shí): 9
    print('總計(jì)耗時(shí):', use_time)

到此這篇關(guān)于Python多線程采集二手房源數(shù)據(jù)信息流程詳解的文章就介紹到這了,更多相關(guān)Python采集二手房源數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • python下對(duì)hsv顏色空間進(jìn)行量化操作

    python下對(duì)hsv顏色空間進(jìn)行量化操作

    這篇文章主要介紹了python下對(duì)hsv顏色空間進(jìn)行量化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • Python入門(mén)教程(三十五)Python中文件的打開(kāi)

    Python入門(mén)教程(三十五)Python中文件的打開(kāi)

    這篇文章主要介紹了Python入門(mén)教程(三十五)Python中文件的打開(kāi),在Python中文件的讀取主要是用open()函數(shù),那么open()函數(shù)有哪些方法呢,今天我們就來(lái)看一看,需要的朋友可以參考下
    2023-05-05
  • Python中執(zhí)行調(diào)用JS的多種實(shí)現(xiàn)方法總結(jié)

    Python中執(zhí)行調(diào)用JS的多種實(shí)現(xiàn)方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于Python中執(zhí)行調(diào)用JS的多種實(shí)現(xiàn)方法,在一些特殊的python應(yīng)用場(chǎng)景下需要逆向執(zhí)行javascript代碼塊或者.js文件,需要的朋友可以參考下
    2023-08-08
  • opencv對(duì)多種顏色小球的形狀及位置判斷方式

    opencv對(duì)多種顏色小球的形狀及位置判斷方式

    在這段時(shí)間參加了一個(gè)競(jìng)賽,寫(xiě)下了這個(gè)代碼,但是總感覺(jué)有一些地方是不完善!這是一個(gè)關(guān)于使用opencv庫(kù)判斷顏色小球形狀及位置的功能實(shí)現(xiàn),其中也參考了一些前輩的代碼,希望能對(duì)迷茫中的小伙幫有所幫助
    2022-11-11
  • Python Pandas describe()函數(shù)的使用詳解

    Python Pandas describe()函數(shù)的使用詳解

    pandas庫(kù)中的describe()函數(shù)為我們提供了這樣的功能,它可以快速生成數(shù)據(jù)集的描述性統(tǒng)計(jì)信息,這篇文章主要介紹了Python Pandas describe()函數(shù)的使用介紹,需要的朋友可以參考下
    2024-05-05
  • python時(shí)間序列按頻率生成日期的方法

    python時(shí)間序列按頻率生成日期的方法

    這篇文章主要介紹了python時(shí)間序列按頻率生成日期的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 詳解pandas DataFrame的查詢方法(loc,iloc,at,iat,ix的用法和區(qū)別)

    詳解pandas DataFrame的查詢方法(loc,iloc,at,iat,ix的用法和區(qū)別)

    這篇文章主要介紹了詳解pandas DataFrame的查詢方法(loc,iloc,at,iat,ix的用法和區(qū)別),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python tkinter中的錨點(diǎn)(anchor)問(wèn)題及處理

    python tkinter中的錨點(diǎn)(anchor)問(wèn)題及處理

    這篇文章主要介紹了python tkinter中的錨點(diǎn)(anchor)問(wèn)題及處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Matplotlib繪圖基礎(chǔ)之文本標(biāo)注詳解

    Matplotlib繪圖基礎(chǔ)之文本標(biāo)注詳解

    Matplotlib?文本和標(biāo)注可以為數(shù)據(jù)和圖形之間提供額外的信息,幫助觀察者更好地理解數(shù)據(jù)和圖形的含義,下面就將通過(guò)示例依次介紹文本和標(biāo)注的常用使用方式
    2023-08-08
  • Python基于FTP模塊實(shí)現(xiàn)ftp文件上傳操作示例

    Python基于FTP模塊實(shí)現(xiàn)ftp文件上傳操作示例

    這篇文章主要介紹了Python基于FTP模塊實(shí)現(xiàn)ftp文件上傳操作,結(jié)合實(shí)例形式分析了Python引入ftp模塊及相關(guān)設(shè)置、文件傳輸?shù)炔僮骷记?需要的朋友可以參考下
    2018-04-04

最新評(píng)論