Python爬虫 您所在的位置:网站首页 链家网二手房官网上海 Python爬虫

Python爬虫

2024-06-20 13:20| 来源: 网络整理| 查看: 265

一、选题的背景

  本次项目选择了中国的一线城市——上海市,通过了解上海市二手房的情况,可以帮助人们在购房、出租等方面做出更明智的决策。可以帮助人们了解上海市经济的发展趋势。随着互联网的发展,越来越多的房地产信息通过网络发布,使用爬虫技术可以方便地收集和分析这些信息。而本次项目选择的数据来源是链家。链家是一家著名的房地产经纪公司,在上海市有着广泛的房地产业务。通过爬取上海市链家发布的二手房信息,可以获得丰富的数据,为分析提供参考。

二、主题式网络爬虫设计方案 1.主题式网络爬虫名称

上海市链家二手房数据爬虫

2.主题式网络爬虫爬取的内容与数据特征分析

上海市链家二手房数据爬虫主要爬取了上海市链家二手房的所在行政区、小区名称、每平方米价格、整套房总价、建筑面积、所在楼层、房屋朝向、关注人数、所在区域、装修类型、房屋亮点和面积区间。

3.主题式网络爬虫设计方案概述(包括实现思路与技术难点)

思路:查看网页的结构,定位目标数据的位置,爬取数据,将数据进行清洗,最后将清洗后的数据进行可视化。

难点:如何应对网站的反爬虫机制,如何爬取多个页面的数据,请求异常的处理

三、主题页面的结构特征分析 1.主题页面的结构与特征分析

目标内容界面:

2.Htmls 页面解析

3.节点(标签)查找方法与遍历方法 

查找方法:使用lxml库中的xpath函数查找

四、网络爬虫程序设计 1.数据爬取与采集 1 import csv 2 import os 3 import random 4 import time 5 import pandas as pd 6 from lxml import etree 7 import requests 8 from retry.api import retry_call 9 10 11 # 异常重试函数,防止网络不稳定导致抓取数据中断等。 12 def retry_request(crawl_url, req_type='get', retry_times=6): 13 while retry_times: 14 retry_times -= 1 15 try: 16 # 请求头 User-Agent 17 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"} 18 resp = retry_call(getattr(requests, req_type), fargs=[crawl_url], 19 fkwargs={"timeout": 60, "headers": headers}, delay=0, tries=1) 20 # 判断请求状态码以及响应是否为空,为空则继续,反之则返回请求体 21 if resp.status_code == 200 and resp.content: 22 return resp.text 23 except Exception as e: 24 print(e) 25 # 重试次数用完,则直接返回异常数据 26 if not retry_times: 27 return '' 28 # 随机休眠 1-3s 29 time.sleep(random.randint(1, 3)) 30 31 32 # 抓取翻页数据 33 def get_pages(url): 34 page_html = retry_request(url) 35 html_obj = etree.HTML(page_html) 36 # xpath获取每个房屋部分 37 ports = html_obj.xpath('//*[@class="info clear"]') 38 lis = [] 39 for port in ports: 40 # 解析详情url 41 try: 42 url = port.xpath('.//*[@class="title"]/a[1]/@href')[0] 43 except: 44 url = '' 45 # 解析总价 46 try: 47 total_price = port.xpath('.//*[@class="totalPrice totalPrice2"]//text()') 48 total_price = ''.join(total_price).strip().replace('\n', '') 49 except: 50 total_price = '' 51 # 解析单价 52 try: 53 unit_price = port.xpath('.//*[@class="unitPrice"]//text()') 54 unit_price = ''.join(unit_price) 55 except: 56 unit_price = '' 57 # 将当前房屋数据加到house列表里 58 list_data = [url, unit_price, total_price] 59 lis.append(list_data) 60 return lis 61 62 63 # 抓取房屋详情信息 64 def get_detail(infos, city_name): 65 de_url = infos[0] 66 page_html = retry_request(de_url) 67 html_obj = etree.HTML(page_html) 68 # 解析所在行政区 69 try: 70 xzu = html_obj.xpath('//*[@class="areaName"]//span[@class="info"]//text()') 71 xzu = ''.join(xzu) 72 xzq = xzu.split('\xa0')[0] 73 except Exception as e: 74 xzq = '' 75 # 解析所在地址 76 try: 77 szqy = html_obj.xpath('//*[@class="areaName"]//span[@class="info"]//text()') 78 szqy = ''.join(szqy) 79 szqy = szqy.split('\xa0')[1:] 80 szqy = ''.join(szqy).strip() 81 except Exception as e: 82 szqy = '' 83 # 解析小区名称 84 try: 85 xqmc = html_obj.xpath('//*[@class="communityName"]/a[1]//text()') 86 xqmc = ''.join(xqmc).strip() 87 except Exception as e: 88 xqmc = '' 89 # 解析所在楼层 90 try: 91 szlc = html_obj.xpath('//*[@class="base"]//ul/li[2]//text()') 92 szlc = ''.join(szlc).replace('所在楼层', '').strip() 93 except Exception as e: 94 szlc = '' 95 # 解析房屋面积 96 try: 97 house_area = html_obj.xpath('//*[@class="area"]/div[@class="mainInfo"]//text()') 98 house_area = ''.join(house_area).strip() 99 except Exception as e: 100 house_area = '' 101 # 解析房屋朝向 102 try: 103 fwcx = html_obj.xpath('//span[text()="房屋朝向"]/parent::*/text()') 104 fwcx = ''.join(fwcx) 105 fwcx = fwcx.replace('房屋朝向', '').strip() 106 except Exception as e: 107 fwcx = '' 108 # 解析关注人数 109 try: 110 gzrs = html_obj.xpath('//*[@id="favCount"]/text()')[0] 111 except Exception as e: 112 gzrs = '' 113 # 解析房屋核心卖点 114 try: 115 house_good = ''.join(html_obj.xpath('//div[text()="核心卖点"]/following-sibling::div[1]//text()')) 116 house_good = house_good.strip() 117 except Exception as e: 118 house_good = '' 119 # 解析装修状态 120 try: 121 zx_status = ''.join(html_obj.xpath('//span[text()="装修情况"]/parent::*/text()')) 122 zx_status = zx_status.strip() 123 except Exception as e: 124 zx_status = '' 125 # 获取房屋单价 126 unit_price = infos[1] 127 # 获取房屋总价 128 total_price = infos[2]

 

2.对数据进行清洗和处理 1 # 清洗数据 2 def clean_data(): 3 # 将 每平方米价格 清洗成整数 4 df['每平方米价格'] = df.每平方米价格.str.replace('[^\d]', '',regex=True) 5 df['每平方米价格'] = df.每平方米价格.astype('int') 6 # 将 每平方米价格 清洗成小数,并生成价格区间列 7 df['建筑面积'] = df.建筑面积.str.replace('[^\d\.]', '',regex=True) 8 df['建筑面积'] = df.建筑面积.astype('float') 9 df['面积区间'] = df.建筑面积.apply(lambda x: '20㎡以下' if x


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有