python爬取贝壳中二手房的数据 前言通过代码爬取贝壳中二手房的数据以此给更多需要了解爬虫或者二手房信息的人提供便利。第一部分爬取地址1.1贝壳首页地址jiujiang.ke.com第二部分爬取数据2.1输入要爬多少页int(input(输入一共要多少页输入整数))2.2找到要请求的网址wangzhi fhttps://jiujiang.ke.com/ershoufang/pg{y}/2.3定位多个房源信息位置page.eles(xpath://div[classinfo clear], timeout20)2.4设置要爬的信息​ for x in loc: # 标题 biaoti x.ele(xpath:.//div[classtitle]/a).text # 地址 address x.ele(xpath:.//div[classpositionInfo]/a).text # 概述 info x.ele(xpath:.//div[classhouseInfo]).text # 总价 price x.ele(xpath:.//div[classtotalPrice totalPrice2]/span).text ​ # 详情页链接 lianjie x.ele(xpath:.//div[classpositionInfo]/a).attr(href)第三部分保存信息pandas.DataFrame(kong_list).to_csv(贝壳二手房九江.csv, indexFalse)全部的代码见文章末尾更多信息请关注......本篇文章的源码如下from DrissionPage import ChromiumPage import time import pandas kong_list [] num int(input(输入一共要多少页输入整数)) for y in range(1, num1): page ChromiumPage() wangzhi fhttps://jiujiang.ke.com/ershoufang/pg{y}/ page.get(wangzhi) # 遇到需要验证的页码就跳过进入下一页 loc page.eles(xpath://div[classinfo clear], timeout20) # 定位多个房源信息的位置 for x in loc: # 标题 biaoti x.ele(xpath:.//div[classtitle]/a).text # 地址 address x.ele(xpath:.//div[classpositionInfo]/a).text # 概述 info x.ele(xpath:.//div[classhouseInfo]).text # 总价 price x.ele(xpath:.//div[classtotalPrice totalPrice2]/span).text # 详情页链接 lianjie x.ele(xpath:.//div[classpositionInfo]/a).attr(href) print(\n, biaoti, address, info, price, lianjie) kong_list.append({ 标题: biaoti, 地址: address, 概述: info, 总价: price万, 详情链接: lianjie, }) time.sleep(3) # 写入文件 pandas.DataFrame(kong_list).to_csv(贝壳二手房九江.csv, indexFalse)