
前言通過代碼爬取貝殼中二手房的數據以此給更多需要了解爬蟲或者二手房信息的人提供便利。第一部分爬取地址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)