爬取自如网站杭州市的租房信息

爬取自如网站杭州市的租房信息

最近看到自如网的整体网页结构比较简洁,因此尝试获取一下杭州市的租房情况,简单做一个分析。

  • 需要获取的内容如图所示

1389345-20180429105357196-700117606.jpg

1.获取网页内容
web_url='http://hz.ziroom.com/z/nl/z2.html?p=1'
#对一些需要登录的网站,如果不是从浏览器发出的请求,得不到响应,所以需要将爬虫程序发出的请求伪装成浏览器。
headers ={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) '
                                    'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132                                   Safari/537.36'}
req=urlrequest.Request(url=web_url,headers=headers)
web_page=urlrequest.urlopen(req).read()
content=web_page.decode('utf8') #中文解码
#print(content)
2.找到有效信息所在标签
soup=BeautifulSoup(content,'html.parser')
soup_valid=soup.find(id="houseList")
all_divs=soup_valid.find_all('li')
print(all_divs[0].prettify())
  • 获取内容的部分截图

标签截图

#符合要求的标签数量
len(all_divs)

输出:

18

3.整理标签内容
print('apartment|area|size|dis_to_subway|price')
for div in all_divs:
    apartment=div.find(class_="t1").get_text().split()[2]
    area=div.find('h4').get_text()
    size=div.find(class_="detail").find('span').get_text()
    dis_to_subway=div.find(class_="detail").find_all('span')[3].get_text()
    price=div.find(class_="price").get_text().split()[1]
    print('{},{},{},{},{}'.format(apartment,area,size,dis_to_subway,price))
  • 单页爬取结果打印

单页爬取结果

爬取杭州市所有租房信息,并存入文件的完整代码如下:
import urllib.request as urlrequest
from bs4 import BeautifulSoup
import time

start=time.clock()
web_url='http://hz.ziroom.com/z/nl/z2.html?p={}'
headers ={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) '
                                    'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
#把租房信息存入文件中
with open('rent_data.csv','w')as outfile:
    outfile.write('apartment,area,size,dis_to_subway,price\n')
    #一共50页
    for i in range(1,51,1):
        req=urlrequest.Request(url=web_url.format(i),headers=headers)
        web_page=urlrequest.urlopen(req).read()
        content=web_page.decode('utf8')
        #找到有效信息所在标签
        soup=BeautifulSoup(content,'html.parser')
        soup_valid=soup.find(id="houseList")
        all_divs=soup_valid.find_all('li')
        for div in all_divs:
            apartment=div.find(class_="t1").get_text().split()[2]
            area=div.find('h4').get_text()
            size=div.find(class_="detail").find('span').get_text()
            dis_to_subway=div.find(class_="detail").find_all('span')[3].get_text()
            price=div.find(class_="price").get_text().split()[1]
            outfile.write('{},{},{},{},{}\n'.format(apartment,area,size,dis_to_subway,price))
end=time.clock()
print('time cost:%.3f s' % (end-start)) #时间消耗

输出:

time cost:37.350 s

转载于:https://www.cnblogs.com/toastavocado/p/8970584.html

贝壳租房(Zillow in China)通常是指链家、自如等在线房地产平台,用于查找房源信息。如果你想要分区域爬取这些平台的数据,比如标题、房源链接和描述,你需要利用网络爬虫技术,结合HTML解析库(如Python的BeautifulSoup或Scrapy框架),以及HTTP请求库(如requests)。这里提供一个简单的Python脚本概念: ```python import requests from bs4 import BeautifulSoup # 定义要爬取的地区列表 regions = ['朝阳区', '海淀区', '丰台区'] # 替换为你想爬取的区域 def crawl_region(region): base_url = "https://bj.ziroom.com/search" # 贝壳租房首页地址,实际需查看API文档 params = { 'sort': 'newest', # 排序方式,可根据需求调整 'location': region, } headers = { 'User-Agent': 'Your User Agent' # 设置用户代理,防止被识别为机器人 } response = requests.get(base_url, params=params, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') # 找到包含房源信息的部分并提取数据 for listing in soup.find_all('div', class_='list-item'): # 这里假设class='list-item'是房源列表元素 title = listing.h3.text.strip() # 提取标题 link = base_url + listing.a['href'] # 提取房源链接 desc = listing.p.text.strip() # 提取描述,可能需要进一步处理HTML标签 print(f"标题: {title}") print(f"链接: {link}") print(f"描述: {desc}\n") # 爬取每个区域 for region in regions: crawl_region(region) # 开始爬取 crawl_region(regions[0]) ``` 注意,这个示例代码仅供参考,实际操作时可能会因为网站结构的变化而失效。同时,许多网站都有反爬虫策略,因此在爬取之前应检查网站的robots.txt文件,并遵守其规定。如果直接抓取可能导致IP被封禁,最好使用代理IP池或者设置延时。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值