zoukankan      html  css  js  c++  java
  • 广州楼盘抓取分析1-数据抓取

    抓取安居客新开楼盘的数据,简单写了个爬虫,没有用什么框架,页面解析用了pyquery,对于有中文的html,解析起来清晰且省事。

    # -*- coding: utf-8 -*-
    #######################################################################
    # Copyright (C) 2005-2016 UC Mobile Limited. All Rights Reserved
    # File          : first_sale_spider.py
    #
    # Creation      : 2016/2/23 19:41
    # Author        : shufeng.lsf@ucweb.com
    #######################################################################
    
    import requests
    import re
    from pyquery import PyQuery as pq
    import MySQLdb
    import uniout
    import sys
    reload(sys)
    sys.setdefaultencoding("utf-8")
    
    community_list = []
    
    HOST = "127.0.0.1"
    USER = "root"
    PASSWD = "root"
    DB = "house_analysis"
    PORT = 3306
    
    
    class DBOperate(object):
        def __init__(self, host, user, passwd, db, port, charset="utf8"):
            self.host = host
            self.user = user
            self.passwd = passwd
            self.db = db
            self.port = port
            self.conn = MySQLdb.connect(self.host, self.user, self.passwd, self.db, self.port, charset="utf8")
            self.cur = self.conn.cursor()
    
        def insertSql(self,sql):
            self.cur.execute(sql)
            self.conn.commit()
    
        def __del__(self):
            self.cur.close()
            self.conn.close()
    
    
    def requestByGet(url):
        r = requests.get(url)
        return r.content
    
    def getNextPage(content):
        m = re.findall(r'<a href="(.+?)" class="next-page next-link">下一页</a>',content)
        if len(m)>0:
            next_url = m[0]
        else:
            next_url = ''
        return next_url
    
    def getCommunityList(content):
        community_urls = re.findall(r'data-link="(http://gz.fang.anjuke.com/loupan/d+?.html)"',content)
        print "正在采集...",community_urls
        if len(community_urls)>0:
            community_list.extend(community_urls)
    
    def handler(start_url):
        while True:
            content = requestByGet(start_url)
            next_url = getNextPage(content)
            getCommunityList(content)
            if next_url != '':
                start_url = next_url
                continue
            else:
                break
    
    
    def getHouseInfo(url):
        p = pq(url)
        name = p('h1').text().strip()
        style = p('.house-item').text().split(",")[0].strip()
        price = p('.sp-price').text().strip()
        l = p('.lpAddr-text').text()
        location = re.split('[ | ]',l)
        area = location[-2].split('-')[0].strip()
        zone = location[-2].split('-')[1].strip()
        address = location[-1].strip()
        detail_location = location[-1].strip()
        result = {
            "name": name,
            "area": area,
            "location": zone,
            "detail_location": detail_location,
            "house_style": style,
            "price": price
        }
        return result
    
    
    def detailPageHandler(cur, detail_url):
        result = getHouseInfo(detail_url)
        print "result:",result
        cur.insertSql("insert into first_sale (name,area,location,detail_location,house_style,price) VALUES('%s','%s','%s','%s','%s','%d')" % (
            result['name'],
            result['area'],
            result['location'],
            result['detail_location'],
            result['house_style'],
            int(result['price'])
        ))
    
    
    def main():
        # handler("http://gz.fang.anjuke.com/loupan/?from=navigation")
        # print community_list
        # print "len(community_list)",len(community_list)
        community_list = ['http://gz.fang.anjuke.com/loupan/313562.html']
        cur = DBOperate(host=HOST, user=USER, passwd=PASSWD, db=DB, port=PORT)
        for each_detail in community_list:
            detailPageHandler(cur, each_detail)
    
    if __name__ == '__main__':
        main()
    

      

    抓取结果如下:

    mysql> select * from first_sale;
    +-----+--------------------------------+--------------+-----------------+--------------------------------------------------------------------------------------------------+------------------------------------------------+--------+
    | id  | name                           | area         | location        | detail_location                                                                                  | house_style                                    | price  |
    +-----+--------------------------------+--------------+-----------------+--------------------------------------------------------------------------------------------------+------------------------------------------------+--------+
    |  10 | 中国美林湖                     | 花都         | 狮岭            | 山前大道168号                                                                                    | 一室(6) , 二室(14) , 三室(15)                | 6500   |
    |  11 | 龙湖首开天宸原著               | 天河         | 车陂            | 奥体路转悦景路600米                                                                              | 三室(4) , 四室(2) , 别墅(2)                  | 30000  |
    |  12 | 雅居乐剑桥郡                   | 番禺         | 华南            | 南村镇草堂村地段                                                                                 | 三室(8) , 四室(7)                             | 20750  |
    |  13 | 碧桂园凤凰城                   | 增城         | 广园东          | 快速路                                                                                           | 一室(1) , 二室(3) , 三室(10)                 | 13000  |
    |  14 | 南沙金茂湾                     | 南沙         | 南沙            | 凤凰大道外国语学院旁                                                                             | 三室(2) , 四室(2) , 商住(3)                  | 15000  |
    |  15 | 珠江佳滨苑                     | 海珠         | 东晓            | 海印桥南东晓路与仲恺路交界                                                                       | 二室(3) , 三室(6)                             | 32000  |
    |  16 | 时代长岛                       | 南沙         | 南沙            | 进港大道377号(南沙街道旁)                                                                      | 二室(1) , 三室(2)                             | 9000   |
    |  17 | 中恒海晖花园                   | 广州周边     | 佛山            | 里水和顺黄金主轴里和大道旁                                                                       | 三室(4)                                        | 7500   |
    |  18 | 南沙涉外公馆                   | 南沙         | 南沙            | 进港大道579号                                                                                    | 商住(3)                                        | 13000  |
    |  19 | 金地香山湖                     | 增城         | 新塘            | 新新公路15号(广园东路中新出口)                                                                 | 三室(1) , 四室(2)                             | 7300   |
    |  20 | 新城国际寓                     | 海珠         | 工业大道        | 江南大道南与工业大道南交汇(第一金碧旁)                                                         | 商住(4)                                        | 25000  |
    |  21 | 金融街·融穗澜湾                | 番禺         | 市桥            | 盛泰路与长提东路交界,黄沙岛花园北面                                                             | 三室(3) , 四室(1)                             | 20000  |
    |  22 | 江畔雅苑                       | 越秀         | 北京路          | 太平沙牌坊直入(地铁6号线北京路站A出口)                                                         | 一室(1) , 二室(2) , 三室(2)                  | 42000  |
    |  23 | 时代春树里                     | 黄埔         | 萝岗            | 开源大道植树公园旁                                                                               | 三室(2) , 四室(1)                             | 14000  |
    |  24 | 雅居乐凯茵又一城               | 广州周边     | 其他            | 火炬开发区(凯茵新城纪念中学旁)                                                                 | 三室(1) , 四室(1)                             | 6000   |
    |  25 | 时尚广场                       | 增城         | 新塘            | 港口大道北395号                                                                                  | 二室(2) , 三室(2) , 四室(1)                  | 11000  |
    |  26 | 时代廊桥增城                   | 增城         | 新塘            | 永宁大道南侧                                                                                     | 二室(1) , 三室(2) , 四室(1)                  | 8800   |
    |  27 | 金融街·融穗御府                | 黄埔         | 萝岗            | 长岭路永顺大道(新建长平小学南侧)                                                               | 三室(5)                                        | 13500  |
    |  28 | 保利西悦湾                     | 荔湾         | 芳村            | 东漖南路(芳村花园对面)                                                                         | 三室(2) , 四室(1)                             | 25000  |
    |  29 | 御华园                         | 花都         | 狮岭            | 狮岭大道东1号                                                                                    | 一室(1) , 二室(9) , 三室(4)                  | 7200   |
    |  30 | 奥园康威广场                   | 增城         | 新塘            | 荔新大道和香山大道交汇处                                                                         | 商住(2)                                        | 11000  |
    |  31 | 恒大御景湾                     | 白云         | 金沙洲          | 金沙洲大桥沿江北段                                                                               | 二室(6) , 三室(4) , 四室(3)                  | 171    |
    |  32 | 珠江帝景紫龙府                 | 海珠         | 广州大道南      | 新领事馆东侧                                                                                     | 四室(2) , 五室及以上(3)                       | 42000  |
    |  33 | 万科金色梦想                   | 黄埔         | 萝岗            | 科学城开创大道与开源大道交接处                                                                   | 二室(1) , 三室(9) , 四室(1) 
    

      

  • 相关阅读:
    double 和 int 同时存在时的运算
    快速排序
    案例:商品放大镜效果
    淘宝flexible.js源码分析
    案例:模态框拖拽
    Web APIs——BOM
    案例:获取URL参数数据
    案例:5秒之后自动跳转页面
    JS中this指针的指向
    按钮:点击发送短信按钮60秒内不能再次点击的功能
  • 原文地址:https://www.cnblogs.com/alexkn/p/5215585.html
Copyright © 2011-2022 走看看