python爬虫demo——爬取历史平均房价

简单爬取历史房价

需求

爬取的网站汇聚数据的城市房价

https://fangjia.gotohui.com/

功能

选择城市

https://fangjia.gotohui.com/fjdata-3

需要爬取年份的数据,等等

https://fangjia.gotohui.com/years/3/2018/

使用bs4模块

使用bs4模块快速定义需要爬取的表格

代码

python 复制代码
from urllib.request import urlopen

import pandas as pd
from bs4 import BeautifulSoup
import urllib.request
import time


headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54'}

# 发送网络请求获取网页内容
def get_page_data(data_url, headers):
    req = urllib.request.Request(data_url, headers=headers)
    content = urllib.request.urlopen(req).read()  # .decode('GBK')
    content = content.decode('utf-8')  # python3
    page = BeautifulSoup(content, 'html.parser')
    return page

# 按格式输出价格
def get_date(date, year):
    date_str = ''
    if date == '1月':
        date_str = year + '-' + '01'
    elif date == '2月':
        date_str = year + '-' + '02'
    elif date == '3月':
        date_str = year + '-' + '03'
    elif date == '4月':
        date_str = year + '-' + '04'
    elif date == '5月':
        date_str = year + '-' + '05'
    elif date == '6月':
        date_str = year + '-' + '06'
    elif date == '7月':
        date_str = year + '-' + '07'
    elif date == '8月':
        date_str = year + '-' + '08'
    elif date == '9月':
        date_str = year + '-' + '09'
    elif date == '10月':
        date_str = year + '-' + '10'
    elif date == '11月':
        date_str = year + '-' + '11'
    elif date == '12月':
        date_str = year + '-' + '12'
    return date_str


# 使用bs4内网页内容进行提取
def analyse_data(page, year):
    table = page.find('table', attrs={'class': 'ntable table-striped'})
    trs = table.find_all('tr')[3:]
    df_data = pd.DataFrame(columns=['date', 'price'])
    time.sleep(1)
    count = 0

    for tr in trs:
        tds = tr.find_all('td')
        date = tds[0].text
        date = get_date(date,year)
        new = tds[1].text
        new = new[:6]
        df_data.loc[count] = [date, new]
        count += 1
    return df_data


if __name__ == '__main__':

    data_url = 'https://fangjia.gotohui.com/fjdata-3'
    year = ['2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023']
    all_datas = []
    file_path = "data.txt"

    # 遍历多年的数据
    for i in year:
        url = 'https://fangjia.gotohui.com/years/3/' + i + '/'
        page = get_page_data(url, headers)
        df_data = analyse_data(page, i)
        print(df_data)
        # 将数据保存到txt文件文件中,(存在编码问题后续解决)
        df_data1 = str(df_data)
        with open(file_path, 'a',encoding='utf-8') as file:
            file.write(df_data1)

运行效果

保存到文件

相关推荐
今天没有盐18 小时前
Pandas完全指南:从Series到DataFrame,掌握数据分析核心技能
python·pycharm·编程语言
暴风鱼划水18 小时前
算法题(Python)数组篇 | 4.长度最小的子数组
python·算法·力扣
小龙报18 小时前
《算法通关指南数据结构和算法篇(4)--- 队列和queue》
c语言·开发语言·数据结构·c++·创业创新·学习方法·visual studio
民乐团扒谱机18 小时前
深入浅出理解克尔效应(Kerr Effect)及 MATLAB 仿真实现
开发语言·matlab·光学·非线性光学·克尔效应·kerr effect
B站计算机毕业设计之家18 小时前
大数据python招聘数据分析预测系统 招聘数据平台 +爬虫+可视化 +django框架+vue框架 大数据技术✅
大数据·爬虫·python·机器学习·数据挖掘·数据分析
7澄118 小时前
深入解析 LeetCode 数组经典问题:删除每行中的最大值与找出峰值
java·开发语言·算法·leetcode·intellij idea
计算衎18 小时前
.c .o .a .elf .a2l hex map 这些后缀文件的互相之间的联系和作用
开发语言·elf·gcc·c/c++·a2l
ysyxg18 小时前
设计模式-策略模式
java·开发语言
新手村领路人19 小时前
python打包成exe
python·打包
胡桃不是夹子19 小时前
torch和torchvision对应版本匹配官网下载
人工智能·python·深度学习