Python爬虫案例:从某居网爬取房源信息

网站链接:sjz.anjuke.com

目标数据:位置、面积、价格、房源链接

约束条件:房产价格在80-140w

首先在浏览器上输入网址,通过鼠标右键-“检查”来确定各网页元素在html源代码中的位置和构成

通过检查导航的价格索引,找出了80-140w的房源信息的网页链接,url依次以13-15结尾并且其它数据保持一致 

进入其中一个价格区间的网页,观察到目标数据在一个 tongji_tag=fcpc_ersflist_gzcount、class=property的div标签中且网页存在若干分页

掌握了目标网页的构成之后就可以开始编写对应的程序了

首先引入需要的python模块并且初始化一些数据,占位符“%d”代表不同网页变化的特征序号

import requests
from bs4 import BeautifulSoup
url = "https://sjz.anjuke.com/sale/m172%d-p%d/"
head = {'User-Agent': ''}  # 字典的值从标头获取

然后根据目标网页构建相应范围的循环,我们的目标网站url尾数从13-15且每个价格区间的网站有不知数目的若干分页,在遍历分页时用while循环和try语句以便在分页结束时跳出循环

for i in range(13, 16):
    num = 1
    while True:
        try:
           '''以下是获取数据的逻辑'''

           '''代码结束'''
        except Exception as e:
            print(e)
            break
        num += 1

之后编写爬取房源价格、面积、地点、对应链接的逻辑:首先通过填充占位符得到正确的url 并解析,通过find、find_all语句获取包含目标数据的所有大标签并存入一个元组,遍历该元组并获取每一个大标签的目标数据(在遇到class、id相同的兄弟标签时用.contents方法获取其父母的所有儿子再用索引获取目标标签)

html = requests.get(url % (i, num), headers=head).text
soup = BeautifulSoup(html, 'html.parser')
arr = (soup.find('div', id='__nuxt').find('div', id='__layout').find('div', id='esfMain').find('section',class_='list-body').find('section', class_='list-main').find('section', class_='list-left').find('section',class_='list').findAll('div',class_='property'))
for item in arr:
    link=item.find('a')['href']
    price=item.find('div',class_='property-content').find('div',class_='property-price').find('p',class_='property-price-total').find('span',class_='property-price-total-num').get_text(strip=True)
    area=item.find('div',class_='property-content').find('div',class_='property-content-detail').find('section').find('div',class_='property-content-info').contents[2].get_text(strip=True)
    pos=item.find('div',class_='property-content').find('div',class_='property-content-detail').find('section').find('div',class_='property-content-info property-content-info-comm').get_text(strip=True)

最后将数据存入txt文件即可

 message=str(count)+"、"+"价格:"+str(price)+"万"+" "+"面积:"+str(area)+" "+"地址:"+str(pos)+" "+str(link)
                with open("D:/hourse.txt", "a") as f:#填存储文件的地址
                    f.write(message)
                    f.close()

 

 注意再爬取过程中出现nonetype object has no attribute "find"可能是爬取次数过多网站跳出了图形验证码或者是写代码时粗心填错了find中的参数,网站跳出验证码时手动填写验证码即可或者少量多次获取数据来应对反爬机制

完整代码

import requests
from bs4 import BeautifulSoup
url = "https://sjz.anjuke.com/sale/m172%d-p%d/"
head = {'User-Agent': ''}  # 字典的值从标头获取
count=1
for i in range(13, 16):
    num = 1
    while True:
        try:
            html = requests.get(url % (i, num), headers=head).text
            soup = BeautifulSoup(html, 'html.parser')
            arr = (soup.find('div', id='__nuxt').find('div', id='__layout').find('div', id='esfMain').find('section',class_='list-body').find('section', class_='list-main').find('section', class_='list-left').find('section',class_='list').findAll('div',class_='property'))
            for item in arr:
                link=item.find('a')['href']
                price=item.find('div',class_='property-content').find('div',class_='property-price').find('p',class_='property-price-total').find('span',class_='property-price-total-num').get_text(strip=True)
                area=item.find('div',class_='property-content').find('div',class_='property-content-detail').find('section').find('div',class_='property-content-info').contents[2].get_text(strip=True)
                pos=item.find('div',class_='property-content').find('div',class_='property-content-detail').find('section').find('div',class_='property-content-info property-content-info-comm').get_text(strip=True)
                message=str(count)+"、"+"价格:"+str(price)+"万"+" "+"面积:"+str(area)+" "+"地址:"+str(pos)+" "+str(link)
                with open("", "a") as f:#填自己的存储地址
                    f.write(message)
                    f.close()
        except Exception as e:
            print(e)
            break
        num += 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值