Python实战学习笔记:爬取租房网站信息

第一个爬虫尝试,爬取小猪短租上海地区10页所有的房屋信息
首先爬取一个房间的基本信息,包括标题、地址、价格、图片、房东基本信息,代码如下:

    url = 'http://sh.xiaozhu.com/fangzi/1863532734.html'
    wb_data = requests.get(url)
    # 开始解析网页数据
    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)
    # 解析网页
    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)]

最后将各个模块写成函数,通过函数调用实现抓取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属性内容不同,通过这个差异区分房东性别
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)
    # 解析网页
    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)
    # 开始解析网页数据
    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:
    # 把得到的列表页面链接,传给函数,这个函数可以得到详情页链接
    get_links(single_url)

爬取结果如下图:

Python实战学习笔记:爬取租房网站信息_第1张图片
Paste_Image.png

总结:
1、在爬取房东性别时,因为信息实在房东头像右下角,且只是一个图标,所有需要通过判断来确定房东的性别
2、获取所有标签时要主要观察网页中标签位置

你可能感兴趣的:(Python实战学习笔记:爬取租房网站信息)