Python爬蟲入門教程03:二手房數據爬取


前言

本文的文字及圖片來源於網絡,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理。

前文內容

Python爬蟲入門教程01:豆瓣Top電影爬取

Python爬蟲入門教程02:小說爬取

PS:如有需要 Python學習資料 以及 解答 的小伙伴可以加點擊下方鏈接自行獲取
python免費學習資料以及群交流解答點擊即可加入

基本開發環境

  • Python 3.6
  • Pycharm

相關模塊的使用

  • requests
  • parsel
  • csv

安裝Python並添加到環境變量,pip安裝需要的相關模塊即可。

一、明確需求

目標需求
爬取圖上所框的內容

二、請求網頁

打開開發者工具( F12或者鼠標右鍵點擊檢查 )選擇 notework 查看數據返回的內容。
在這里插入圖片描述
通過開發者工具可以看到,網站是靜態網頁數據,請求url地址是可以直接獲取數據內容的。

url = 'https://cs.lianjia.com/ershoufang/'
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)
print(response.text)

如果你不知道,返回的數據中是否有你想要的內容,你有復制網頁的內容,在pycharm的輸出結果中進行搜索查看。
在這里插入圖片描述

三、解析數據

既然網站是靜態網頁數據,那么就可以直接在開發者工具中 Elements 查看數據在哪
在這里插入圖片描述
如上圖所示,相關的數據內容都包含在 li 標簽里面。通過 parsel 解析庫,進行解析提取數據就可以了。

selector = parsel.Selector(response.text)
lis = selector.css('.sellListContent li')
for li in lis:
    # 標題
    title = li.css('.title a::text').get()
    # 地址
    positionInfo = li.css('.positionInfo a::text').getall()
    # 小區
    community = positionInfo[0]
    # 地名
    address = positionInfo[1]
    # 房子基本信息
    houseInfo = li.css('.houseInfo::text').get()
    # 房價
    Price = li.css('.totalPrice span::text').get() + '萬'
    # 單價
    unitPrice = li.css('.unitPrice span::text').get().replace('單價', '')
    # 發布信息
    followInfo = li.css('.followInfo::text').get()
    dit = {
        '標題': title,
        '小區': community,
        '地名': address,
        '房子基本信息': houseInfo,
        '房價': Price,
        '單價': unitPrice,
        '發布信息': followInfo,
    }
    print(dit)

當我運行的時候發現報錯了。
在這里插入圖片描述
IndexError: list index out of range 超出索引范圍了。
遇事不要慌, 取0超出索引范圍,說明數據並沒有取到,所以我們要看一下 <精裝好房...> 這個信息下面那一個是什么情況。
在這里插入圖片描述
搜索發現,這個中間插入了一條廣告,也是li標簽里面的,所以做一個簡單的判斷就好了,它是一個廣告並沒有標題,判斷是否有標題就可以了,有就爬取相關內容,沒有就pass掉。

for li in lis:
    # 標題
    title = li.css('.title a::text').get()
    if title:
        # 地址
        positionInfo = li.css('.positionInfo a::text').getall()
        # 小區
        community = positionInfo[0]
        # 地名
        address = positionInfo[1]
        # 房子基本信息
        houseInfo = li.css('.houseInfo::text').get()
        # 房價
        Price = li.css('.totalPrice span::text').get() + '萬'
        # 單價
        unitPrice = li.css('.unitPrice span::text').get().replace('單價', '')
        # 發布信息
        followInfo = li.css('.followInfo::text').get()
        dit = {
            '標題': title,
            '小區': community,
            '地名': address,
            '房子基本信息': houseInfo,
            '房價': Price,
            '單價': unitPrice,
            '發布信息': followInfo,
        }
        print(dit)

在這里插入圖片描述
這樣就不會報錯了。

四、保存數據(數據持久化)

和爬取豆瓣的電影信息是一樣的,使用csv模塊,把數據保存到Excel里面

# 創建文件
f = open('二手房數據.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['標題', '小區', '地名', '房子基本信息',
                                           '房價', '單價', '發布信息'])
# 寫入表頭
csv_writer.writeheader()
''''
''''
csv_writer.writerow(dit)

五、多頁爬取

# 第二頁url地址
url_2 = 'https://cs.lianjia.com/ershoufang/pg2/'
# 第三頁url地址
url_3 = 'https://cs.lianjia.com/ershoufang/pg3/'
# 第四頁url地址
url_4 = 'https://cs.lianjia.com/ershoufang/pg4/'

在這里插入圖片描述
通過以上的內容,只需要for 循環遍歷 pg的參數 即可多頁爬取

for page in range(1, 101):
    url = f'https://cs.lianjia.com/ershoufang/pg{page}/'

這樣就可以進行多頁爬取了。

實現效果

在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM