58同城租房房源信息获取

五八房源信息获取

爬取房源信息算是比较常见的且稍微有些难度的,今日分享关于五八租房信息的爬取,仅作参考,抛砖引玉

房源链接

上海五八租房

目标定位

  • 获取上海租房信息
  • 保存为csv文件

部分核心代码演示

导库
import csv
import random
import requests
from lxml import etree
xpath路径(利用xpath定位到要爬取的信息是关键步骤之一)

//div[@class=“house-title”]/h1/text() 标题
//div[@class=“house-pay-way f16”]/span/b/text() 价格
//div[@class=“house-desc-item fl c_333”]/ul/li[1]/span[2]/text() 租赁方式
//div[@class=“house-desc-item fl c_333”]/ul/li[2]/span[2]/text() 房屋类型
//div[@class=“house-desc-item fl c_333”]/ul/li[3]/span[2]/text() 朝向楼层
//div[@class=“house-desc-item fl c_333”]/ul/li[4]/span[2]/a/text() 所在小区
//div[@class=“house-desc-item fl c_333”]/ul/li[5]/span[2]/a[1]/text()所属区域
//div[@class=“house-desc-item fl c_333”]/ul/li[5]/span[2]/a[2]/text() 所在路段
//div[@class=“house-desc-item fl c_333”]/ul/li[@class=“li_br”]/span[2]/text() 详细地址
‘’’

代理池配置(防止反爬,不赘述,最好用自己的租用的代理)

proxies_pool=[
    {'http': '188.132.221.27:8080'},
    {'http': '103.152.232.134:8080'},
    {'http': '103.227.252.102:8080'},
    {'http': '79.106.170.34:8989'},
    {'http': '190.110.99.189:999'},

]

构造请求头

proxies=random.choice(proxies_pool)
headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'

}

自定义输入页面构建csv文件方便写入

base_url='https://sh.58.com/zufang/pn'
i=int(input("请输入起始页:"))
end=int(input("请输入终止页:"))
fp=open('./wuba.csv','a+',encoding='utf-8',newline='')

分析每页的二级块所涵盖的链接并打印

for i in range(i,end+1) :
    # startpage_index+=i
    url=base_url+str(i)+'/'
    print(url) #<--定义页面,选择第几页
    response=requests.get(url=url,headers=headers,proxies=proxies)
    content=response.text
    # print(content)
    tree=etree.HTML(content)
    hrefs=tree.xpath('//div[@class="des"]/h2/a/@href')
    print(hrefs)

之后是逐个获取

 try:
            totalprice=fintree.xpath('//div[@class="house-pay-way f16"]/span/b/text()')[0]
        except IndexError:
            totalprice=''
        # 租赁方式
        try:
            typeofrent=fintree.xpath('//div[@class="house-desc-item fl c_333"]/ul/li[1]/span[2]/text()')[0]
        except IndexError:
            typeofrent=''
        # 小区
        try:
            commname=fintree.xpath('//div[@class="house-desc-item fl c_333"]/ul/li[4]/span[2]/a/text()')[0]
        except IndexError:
            commname=''
        # 所在路段
        try:
            roadinfo=fintree.xpath('//div[@class="house-desc-item fl c_333"]/ul/li[5]/span[2]/a[2]/text()')[0]
        except IndexError:
            roadinfo=''
        # 6房屋类型house_type
        try:
            house_type=fintree.xpath('//div[@class="house-desc-item fl c_333"]/ul/li[2]/span[2]/text()')[0]
        except IndexError:
            house_type=''
        # 所属区域
        try:
            area=fintree.xpath('//div[@class="house-desc-item fl c_333"]/ul/li[5]/span[2]/a[1]/text()')[0]
        except IndexError:
            area=''

写入并保存

writer.writerow(houselst)
        print(houselst)

以上便是部分核心代码,xpath很实用,虽说不如正则那样超精准,但是其瑕不掩瑜,也是非常好用的一种爬虫手段,关于类似于爬虫之后的数据分析以及简单可视化,本人另一个文章有展示(略简陋,见谅),不赘述。还有发布的打包好的链家网爬虫软件,点我主页看另一个博文就能看见。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值