Python爬虫实战,requests+parsel模块,爬取二手房房源信息数据

前言

最近在尝试用Python爬虫二手房房源信息数据,在这里给需要的小伙伴们提供代码,并且给出一点小心得。

首先是爬取之前应该尽可能伪装成浏览器而不被识别出来是爬虫,基本的是加请求头,但是这样的纯文本数据爬取的人会很多,所以我们需要考虑更换代理IP和随机更换请求头的方式来对房价数据进行爬取。

在每次进行爬虫代码的编写之前,我们的第一步也是最重要的一步就是分析我们的网页。

在我们本次的例子中,我们需要在每一页获取每一个具体房源的链接,然后进入到二级网页获取详细的信息,然后再返回上一级网页重复此过程。

通过分析我们发现在爬取过程中速度比较慢,所以我们还可以通过禁用谷歌浏览器图片、JavaScript等方式提升爬虫爬取速度。

开发工具

Python版本: 3.8

相关模块:

requests模块
parsel模块

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

思路分析

爬取页面如下图所示:

在这里插入图片描述

提取页面数据

浏览器中打开我们要爬取的页面
按F12进入开发者工具,查看我们想要的数据在哪里
这里我们需要房源页面数据就可以了
页面数据

代码实现

# 伪装
headers = {
    
    
    'cookie': 'aQQ_ajkguid=B7A0A0B5-30EC-7A66-7500-D8055BFFE0FA; ctid=27; id58=CpQCJ2Lbhlm+lyRwdY5QAg==; _ga=GA1.2.2086942850.1658553946; wmda_new_uuid=1; wmda_uuid=009620ee2a2138d3bd861c92362a5d28; wmda_visited_projects=%3B6289197098934; 58tj_uuid=8fd994c2-35cc-405f-b671-2c1e51aa100c; als=0; ajk-appVersion=; sessid=8D76CC93-E1C8-4792-9703-F864FF755D63; xxzl_cid=2e5a66fa054e4134a15bc3f5b47ba3ab; xzuid=e60596c8-8985-4ab3-a5df-90a202b196a3; fzq_h=4c8d83ace17a19ee94e55d91124e7439_1666957662955_85c23dcb9b084efdbc4ac519c0276b68_2936029006; fzq_js_anjuke_ershoufang_pc=75684287c0be96cac08d04f4d6cc6d09_1666957664522_25; twe=2; xxzl_cid=2e5a66fa054e4134a15bc3f5b47ba3ab; xxzl_deviceid=OOpJsA5XrQMdJFfv71dg+l+he0O1OKPQgRAQcFPbeRAyhjZ4/7gS3Gj4DfiLjxfc; isp=true; obtain_by=2; new_session=1; init_refer=https%253A%252F%252Fcs.anjuke.com%252F; new_uv=3',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
1.发送请求
response = requests.get(url=url, headers=headers)
2.获取数据
html_data = response.text
3.解析数据
 select = parsel.Selector(html_data)
    divs = select.css('.property-content')
    for div in divs:
        # .property-content-title-name   标题
        标题 = is_null(div.css('.property-content-title-name::text').get())
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(1) span  户型
        户型s = div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(1) span::text').getall()
        户型 = ' '.join(户型s)
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(2)  面积
        面积 = is_null(div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(2)::text').get())
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(3)  朝向
        朝向 = is_null(div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(3)::text').get())
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(4)  楼层
        楼层 = is_null(div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(4)::text').get())
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(5)  年份
        年份 = is_null(div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(5)::text').get())
        # .property-content-info:nth-child(2) .property-content-info-comm-name  小区名称
        小区名称 = is_null(div.css('.property-content-info:nth-child(2) .property-content-info-comm-name::text').get())
        # .property-content-info:nth-child(2) .property-content-info-comm-address  小区地址
        小区地址 = is_null(div.css('.property-content-info:nth-child(2) .property-content-info-comm-address::text').get())
        # .property-content-info:nth-child(3) span  小区标签
        小区标签s = div.css('.property-content-info:nth-child(3) span::text').getall()
        小区标签 = ' '.join(小区标签s)
        # .property-price .property-price-total .property-price-total-num  总价
        总价 = is_null(div.css('.property-price .property-price-total .property-price-total-num::text').get())
        # .property-price .property-price-average  每平方米的价格
        单价 = is_null(div.css('.property-price .property-price-average::text').get())
        print(标题, 户型, 面积, 朝向, 楼层, 年份, 小区名称, 小区地址, 小区标签, 总价, 单价)
4.保存数据
        with open('安居客.csv', mode='a', encoding='utf-8', newline='') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow([标题, 户型, 面积, 朝向, 楼层, 年份, 小区名称, 小区地址, 小区标签, 总价, 单价])

结果展示

结果
ps:图片仅供参考

最后

今天的分享到这里就结束了 ,感兴趣的朋友也可以去试试哈

对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦

觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

猜你喜欢

转载自blog.csdn.net/Modeler_xiaoyu/article/details/128181390