溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python如何爬取Q房網(wǎng)數(shù)據(jù)

發(fā)布時(shí)間:2022-01-14 15:23:15 來源:億速云 閱讀:318 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要介紹了python如何爬取Q房網(wǎng)數(shù)據(jù),具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

前言

本文的文字及圖片來源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用,不具有任何商業(yè)用途,版權(quán)歸原作者所有,如有問題請(qǐng)及時(shí)聯(lián)系我們以作處理

本次目標(biāo)

爬取Q房網(wǎng)數(shù)據(jù)

https://shenzhen.qfang.com/newhouse

爬取目標(biāo)數(shù)據(jù):

  • 小區(qū)名字

  • 售房狀態(tài)

  • 房屋面積

  • 戶型

  • 開盤時(shí)間

  • 交房時(shí)間

  • 樓盤地址

  • 售價(jià)

  • 預(yù)計(jì)總價(jià)

python如何爬取Q房網(wǎng)數(shù)據(jù)

emmmm,我看看就行了,買不起買不起

開發(fā)工具

  • python 3.6.5

  • pycharm

爬蟲代碼

導(dǎo)入工具

import requests
import parsel
import csv

解析網(wǎng)頁,爬取數(shù)據(jù)

for page in range(1, 84):
    print('===============================正在爬取第{}頁的數(shù)據(jù)================================================='.format(page))
    url = 'https://shenzhen.qfang.com/newhouse/list/n{}'.format(page)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    selector = parsel.Selector(response.text)
    lis = selector.css('.list-result li')
    dit = {}
    for li in lis:
        title = li.css('.list-main-header a em::text').get()  # 名字
        dit['標(biāo)題'] = title
        status = li.css('.list-main-header i::text').get()     # 是否在售
        dit['房產(chǎn)狀態(tài)'] = status
        space = li.css('.list-main div:nth-child(1) .space span::text').get()     # 售房面積
        dit['售房面積'] = space
        type_list = li.css('.list-main.fl p:nth-child(3) span a::text').getall()     # 戶型
        type_str = '|'.join(type_list).strip().replace('\r\n', '').replace(' ', '')  # 戶型
        dit['戶型'] = type_str
        kp_time = li.css('.new-house-info > div:nth-child(2) > p.space.fl.clearfix > span::text').get()  # 開盤時(shí)間
        dit['開盤時(shí)間'] = kp_time
        cs_time = li.css('.new-house-info > div:nth-child(2) > p:nth-child(3)> span::text').get()  # 出售時(shí)間
        dit['出售時(shí)間'] = cs_time
        address = li.css('.list-main a:nth-child(3)::text').get()  # 地址

        if not address == None:
            address = address.strip()
        else:
            address = None
        dit['地址'] = address
        Price = li.css('.list-price .bigger .amount::text').get()  # 售價(jià)
        dit['售價(jià)'] = Price
        hj_Price = li.css('.list-price .smaller::text').get()   # 預(yù)計(jì)總價(jià)
        dit['預(yù)計(jì)總價(jià)'] = hj_Price

保存數(shù)據(jù)

f = open('房產(chǎn)數(shù)據(jù).csv', mode='a', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['標(biāo)題', '房產(chǎn)狀態(tài)', '售房面積', '戶型', '開盤時(shí)間', '出售時(shí)間', '地址', '售價(jià)', '預(yù)計(jì)總價(jià)'])
csv_writer.writeheader()
print(dit)

運(yùn)行代碼,效果如下圖

python如何爬取Q房網(wǎng)數(shù)據(jù)

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“python如何爬取Q房網(wǎng)數(shù)據(jù)”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI