Python實戰(zhàn)學(xué)習(xí)筆記:爬取租房網(wǎng)站信息

第一個爬蟲嘗試,爬取小豬短租上海地區(qū)10頁所有的房屋信息
首先爬取一個房間的基本信息,包括標題、地址、價格、圖片、房東基本信息,代碼如下:

    url = 'http://sh.xiaozhu.com/fangzi/1863532734.html'
    wb_data = requests.get(url)
    # 開始解析網(wǎng)頁數(shù)據(jù)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    # 獲取標題
    titles = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > h4 > em')
    # 獲取地址
    address = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > p > span.pr5')
    # 獲取日租金
    dayPrices = soup.select('div.day_l > span')
    # 獲取圖片
    imgs = soup.select('#curBigImage')
    # 獲取房東頭像
    fdImgs = soup.select('div.js_box.clearfix > div.member_pic > a > img')
    # 獲取房東性別
    sexs = soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > div')
    # 獲取房東姓名
    names = soup.select('div.js_box.clearfix > div.w_240 > h6 > a')

    for title, addres, dayPrice, img, fdImag, sex, name in zip(titles, address, dayPrices, imgs, fdImgs, sexs, names):
        data = {
            'title': title.get_text(),
            'addres': addres.get_text(),
            'dayPrice': dayPrice.get_text(),
            'img': img.get('src'),
            'fdImg': fdImag.get('src'),
            'sex': get_lorder_sex(sex.get("class")),
            'name': name.get_text()
        }
        print(data)

然后獲取每個頁面中所有的房屋鏈接,代碼:

    wb_data = requests.get(url)
    # 解析網(wǎng)頁
    soup = BeautifulSoup(wb_data.text, 'lxml')
    links = soup.select('#page_list > ul > li > a')
    for link in links:
        href = link.get('href')

然后生成10個列表頁面地址:

urls = ['http://sh.xiaozhu.com/zuipianyi-duanzufang-p{}-10/'.format(number) for number in range(1, 10)]

最后將各個模塊寫成函數(shù),通過函數(shù)調(diào)用實現(xiàn)抓取10個頁面的所有房屋信息
完整源代碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2016/8/1 13:21
# @Author  : flyme
# @Site    : 
# @File    : xiaozhu.py
# @Software: PyCharm Community Edition

from bs4 import BeautifulSoup
import requests

# 性別不同,標簽的class屬性內(nèi)容不同,通過這個差異區(qū)分房東性別
def get_lorder_sex(class_name):
    if class_name == ['member_ico']:
        return '男'
    elif class_name == ['member_ico1']:
        return '女'

# 獲取頁面中所有房屋鏈接
def get_links(url):
    wb_data = requests.get(url)
    # 解析網(wǎng)頁
    soup = BeautifulSoup(wb_data.text, 'lxml')
    links = soup.select('#page_list > ul > li > a')
    for link in links:
        href = link.get('href')
        get_detail_info(href)

# 獲取單個房屋詳細信息
def get_detail_info(url):
    # url = 'http://sh.xiaozhu.com/fangzi/1863532734.html'
    wb_data = requests.get(url)
    # 開始解析網(wǎng)頁數(shù)據(jù)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    # 獲取標題
    titles = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > h4 > em')
    # 獲取地址
    address = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > p > span.pr5')
    # 獲取日租金
    dayPrices = soup.select('div.day_l > span')
    # 獲取圖片
    imgs = soup.select('#curBigImage')
    # 獲取房東頭像
    fdImgs = soup.select('div.js_box.clearfix > div.member_pic > a > img')
    # 獲取房東性別
    sexs = soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > div')
    # 獲取房東姓名
    names = soup.select('div.js_box.clearfix > div.w_240 > h6 > a')

    for title, addres, dayPrice, img, fdImag, sex, name in zip(titles, address, dayPrices, imgs, fdImgs, sexs, names):
        data = {
            'title': title.get_text(),
            'addres': addres.get_text(),
            'dayPrice': dayPrice.get_text(),
            'img': img.get('src'),
            'fdImg': fdImag.get('src'),
            'sex': get_lorder_sex(sex.get("class")),
            'name': name.get_text()
        }
        print(data)

# 生成10個列表頁面地址
urls = ['http://sh.xiaozhu.com/zuipianyi-duanzufang-p{}-10/'.format(number) for number in range(1, 10)]

# 從鏈接列表中,用for一個個取出來
for single_url in urls:
    # 把得到的列表頁面鏈接,傳給函數(shù),這個函數(shù)可以得到詳情頁鏈接
    get_links(single_url)

爬取結(jié)果如下圖:

Paste_Image.png

總結(jié):
1、在爬取房東性別時,因為信息實在房東頭像右下角,且只是一個圖標,所有需要通過判斷來確定房東的性別
2、獲取所有標簽時要主要觀察網(wǎng)頁中標簽位置

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容