python 抓取搜房网数据

本文介绍如何利用BeautifulSoup和re模块抓取搜房网上的新房楼盘信息,包括楼盘名称、位置和价格。
摘要由CSDN通过智能技术生成

本文主要使用BeautifulSoup及re模块对网页内容解析,提取新房楼盘信息主要为名称、位置、价格

代码如下




# coding=utf-8

import os,json,urllib2,requests,zlib,re
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
''' 从新房首页获取城市列表,此处获取不全 '''
def get_text(url):
    res=urllib2.urlopen(url)
    content=zlib.decompress(res.read(),16+zlib.MAX_WBITS)
    soup=BeautifulSoup(content,'lxml')
    city_list=soup.find_all('div',class_='city20141104nr',style='display:none')
    lis=[]
    dic_city={}
    for i in city_list:
        #print len(i.find_all('a'))
        lis+=i.find_all('a')


    for i in lis:
        city_name=str(i).split('>')[1].encode('gbk').strip('</a')
        dic_city[city_name]=i['href']
    return dic_city




'''获取各城市所有页面'''
def get_city_page(city_url):
    res=urllib2.urlopen(city_url)
    content=zlib.decompress(res.read(),16+zlib.MAX_WBITS)
    #html = re.sub("</html>","",content,flags=re.S|re.IGNORECASE)+"</html>"
    soup=BeautifulSoup(conte
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值