资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python爬虫实战之爬取房天下新房数据的示例-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站建设、外贸营销网站建设、醴陵网络推广、重庆小程序开发公司、醴陵网络营销、醴陵企业策划、醴陵品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;创新互联为所有大学生创业者提供醴陵建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

这篇文章将为大家详细讲解有关python爬虫实战之爬取房天下新房数据的示例,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

本示例主要用到requests库和bs4库,requests库用来获取网页内容,bs4库则是解析网页内容,获取有用数据。

代码中url可切换当地房天下网址。

代码如下

# -*- coding:utf-8 -*-
# author:zhoulong
'''
房天下天水新房信息
'''
import requests
from bs4 import BeautifulSoup
import numpy as np
import re
URL = 'http://newhouse.tianshui.fang.com/house/s/b91/'
HTML = requests.get(URL)
SOUP = BeautifulSoup(HTML.content, 'html.parser', from_encoding='gb18030')
last_page = SOUP.select('.last')
page_number = int(last_page[0]['href'].split('/')[3].split('9')[1])#根据尾页划分页码
url_demo = 'http://newhouse.tianshui.fang.com/house/s/b9{}/'#i+1,name.text.strip(),
#房价价格
house_price_list=[]
for i in range(1,(page_number+1)):
    url = url_demo.format(i)
    html = requests.get(url)
    soup = BeautifulSoup(html.content,'html.parser',from_encoding='gb18030')
    names = soup.select('.nlcd_name a')#class定位组合查找
    adresses = soup.select('.address a')#查找地址
    all_type = soup.findAll(name="span", attrs={"class": re.compile(r"forSale|inSale|outSale|zusale|zushou")})#出售
    all_money = soup.findAll(name="div", attrs={"class": re.compile(r"nhouse_price|kanesf")})#价格
    for i,name in enumerate(names):
        print(i+1,' name:'+name.text.strip(),'  address:'+''.join(re.split(r'\s+',
               adresses[i].text.replace('\n','').replace('',''))),
              all_type[i].text,' house_price: '+all_money[i].text.replace('\n',''))
        house_price_list.append(re.findall('\d+',all_money[i].text.replace('\n','')))
house_price_list=[int(i[0]) for i in house_price_list if i]
print('*'*80)
print('* '+' 房价均价:'+str(np.mean(house_price_list))+' '*60+'*')
print('* '+' 房价最高价:'+str(np.max(house_price_list))+' '*60+'*')
print('* '+' 房价最低价:'+str(np.min(house_price_list))+' '*61+'*')
print('*'*80)

执行结果

python爬虫实战之爬取房天下新房数据的示例

python爬虫实战之爬取房天下新房数据的示例

关于python爬虫实战之爬取房天下新房数据的示例就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享文章:python爬虫实战之爬取房天下新房数据的示例-创新互联
当前路径:http://cdkjz.cn/article/degojj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220