selenium链家二手房信息爬取

从链家网爬取指定城市的二手房信息,并将数据保存为CSV文件。它使用了Selenium库来模拟浏览器行为,以便获取动态加载的页面内容。具体而言,它实现了一个名为LianJia的类,其中包含了获取房源列表和房源详情的方法。主要步骤如下:

  • 读取城市信息和代理IP信息,并初始化Chrome浏览器及其设置。
  • 定义LianJia类,其中house_list()方法用于获取指定城区的所有房源链接,house_detail()方法用于获取单个房源的详细信息。
  • run()方法用于执行爬取过程,它首先访问链家网站,然后获取指定城市的所有城区链接,再依次遍历每个城区,获取所有房源的详细信息,并保存到CSV文件中。
  • 最后,循环遍历所有城市,调用run()方法执行爬取任务。
  • 需要注意的是,这段代码在使用Selenium时需要确保已经安装好Chrome浏览器和对应版本的ChromeDriver,并将ChromeDriver的路径添加到系统环境变量中。
python 复制代码
from selenium import webdriver
from fake_useragent import UserAgent
import random
from selenium.webdriver.common.by import By
import os
import pandas as pd
from pinyin import pinyin
import subprocess

# 读取城市信息
with open("city.txt", "r", encoding="utf-8") as file:
    cities = [line.strip() for line in file if line.strip()]

ips = []
with open('ip.txt', 'r') as f:
    for line in f:
        ip = line.strip()
        ips.append(ip.strip())

# 启动Chrome浏览器调试服务
subprocess.Popen('cmd', shell=True)
subprocess.Popen('chrome-win64\chrome.exe" --remote-debugging-port=9222', shell=True)

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("debuggerAddress", "localhost:9222")
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable‐gpu')
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument('--proxy-server=http://' + random.choice(ips))
chrome_options.add_argument(f"user-agent={UserAgent().random}")
driver = webdriver.Chrome(options=chrome_options)

class LianJia:
    def __init__(self):
        # 声明Chrome浏览器对象, 这里填写你自己的driver路径
        self.driver = driver
        self.item = None
    def house_detail(self, item):
        """获取一间房子的详情信息"""
        self.driver.get(item['houseURL'])  # 访问一间房子的详情页

        # 获取页面上的房子信息
        item['title'] = self.driver.find_element(By.TAG_NAME, 'h1').text  # 标题
        item['price'] = self.driver.find_element(By.CSS_SELECTOR, 'span.total').text  # 价格
        house_info = self.driver.find_elements(By.CSS_SELECTOR, 'div.mainInfo')
        item['room'] = house_info[0].text  # 户型
        item['faceTo'] = house_info[1].text  # 朝向
        item['area'] = house_info[2].text  # 面积
        # 小区名
        item['communityName'] = self.driver.find_element(By.CSS_SELECTOR, 'div.communityName a.info').text
        # 发布日期
        item['releaseDate'] = self.driver.find_element(By.XPATH, '//div[@class="transaction"]/div[2]/ul/li/span[2]').text
        
        
    def house_list(self, item):
        """获取一个城区中所有房子的详情页链接"""
        # 访问城区的页面
        self.driver.get(item['partURL'])
        # 切换到'最新发布'页面
        self.driver.find_element(By.LINK_TEXT, '最新发布').click()
        # 获取到所有的房子链接
        house_ls = self.driver.find_elements(By.XPATH, '//ul[@class="sellListContent"]//div[@class="title"]/a')
        # 生成url列表
        house_url_ls = [house.get_attribute("href") for house in house_ls]

        
        # 遍历房子的链接
        for url in house_url_ls:
            item['houseURL'] = url
            self.house_detail(item)

    def run(self, city,folder_path):
        """获取所有城区的页面链接"""
        # 访问二手房网址
        self.driver.get(f'https://{city}.lianjia.com/ershoufang/')
        # 获取所有城区的元素对象
        temp_ls = self.driver.find_elements(By.XPATH, '//div[@class="position"]/dl[2]/dd/div[1]/div/a')
        # 城区名
        part_name_ls = [ele.text for ele in temp_ls]
        # 城区链接
        part_url_ls = [ele.get_attribute("href") for ele in temp_ls]
        item = {}  # 初始化一个容器, 用来存放房子的信息
        for i in range(len(temp_ls)):
            item['partName'] = part_name_ls[i]  # 城区名
            item['partURL'] = part_url_ls[i]  # 城区页面链接
            self.house_list(dict(item))  # 传递深拷贝的item对象

        pd.DataFrame(item).to_csv(folder_path+f'\{city}.csv', index=False, encoding='utf-8')
        self.driver.quit()

    
for city in cities:    
    # 创建目录
    folder_path = os.getcwd()+f"\data\房价\{city}"
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    city1 = pinyin.get_initial(city).replace(" ", "")
    lj = LianJia()
    lj.run(city1,folder_path)
相关推荐
天才测试猿20 小时前
Postman接口测试:如何导入swagger接口文档?
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
计算机学姐20 小时前
基于Python的校园美食推荐系统【2026最新】
开发语言·vue.js·后端·python·mysql·django·推荐算法
天天进步201520 小时前
Python全栈实战:基于机器学习的用户行为分析系统
python
linzeyang20 小时前
Advent of Code 2025 挑战全手写代码 Day 5 - 餐厅
后端·python
祝余Eleanor20 小时前
Day 29 类的定义及参数
人工智能·python·机器学习
码界奇点20 小时前
基于Dash+FastAPI的通用中后台管理系统设计与实现
python·车载系统·毕业设计·fastapi·源代码管理·dash
white-persist20 小时前
【攻防世界】reverse | Mysterious 详细题解 WP
c语言·开发语言·网络·汇编·c++·python·安全
小oo呆21 小时前
【学习心得】Python好库推荐——pipx
linux·开发语言·python
smile_Iris21 小时前
Day 28 元组和OS模块
python·机器学习
AI科技星21 小时前
时空运动的几何约束:张祥前统一场论中圆柱螺旋运动光速不变性的严格数学证明与物理诠释
服务器·数据结构·人工智能·python·科技·算法·生活