搜房网房价小爬虫

本文介绍了一个使用Python进行的福州二手房信息爬取实践,通过BeautifulSoup解析网页,获取房源标题、价格、位置等信息,并利用pymysql将数据存储到MySQL数据库中,同时使用Redis缓存已抓取的数据避免重复爬取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文仅作技术实践验证,禁止用于其它渠道

数据库mysql

CREATE TABLE `fzfang` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(128) DEFAULT NULL,
  `cell` varchar(32) DEFAULT NULL,
  `price` decimal(8,2) DEFAULT NULL,
  `addition` varchar(128) DEFAULT NULL,
  `addtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `href` varchar(64) DEFAULT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=39590 DEFAULT CHARSET=utf8
#!/usr/bin/python36
# -*- coding: utf-8 -*-

from requests import *
import time
import pymysql
from redis import Redis
from bs4 import BeautifulSoup


connection = pymysql.connect(host='localhost',
                             user='fzfang',
                             password='fzfang',
                             db='fang',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

cur = connection.cursor()

redis_conn = Redis(host='localhost', port=6379, db=0)
houses = []

for i in range(1,5):
  r = get('https://fz.esf.fang.com/house/i3'+str(i))
  fangs = BeautifulSoup(r.text, 'html5lib').find_all('dl',class_='clearfix')
  for fang in fangs:
    houses.append(fang)
  time.sleep(1)

#print(houses[0])
for h in houses:
  piece = BeautifulSoup('<html>' + str(h) + '</html>','html5lib')
  title = piece.find('span',class_='tit_shop')
  if(title):
    price = piece.find('span',class_='red').find('b')
    cell = piece.find('p',class_='add_shop').find('a')
    addition = piece.find('p',class_='tel_shop')
    addition = ' '.join(addition.text.split())
    href = piece.find_all('a')[0].get('href')
    if(redis_conn.get( title.text.strip() + price.text.strip() + addition )):
      continue
    else:
      redis_conn.setex(title.text.strip() + price.text.strip() + addition, 5*86400, 1)
      cur.execute('insert into fzfang(title,cell,price,addition,href)values("%s","%s","%s","%s","%s")' % (title.text.strip(),cell.text.strip(),price.text.strip(),addition,href))
      connection.commit()

 

Python爬虫可以用于从搜房网上获取数据。搜房网是一个提供房地产信息的网站,我们可以使用Python爬虫来获取搜房网上的二手房数据。在爬取搜房网数据的过程中,可以使用到一些第三方库和框架,比如requests、BeautifulSoup4和MySQLdb等。具体的流程如下: 1. 确保你已经配置好了Python环境,并安装了相应的库和框架,比如Python2.7、scrapy、lxml、pymysql和MySQL8.0等。 2. 使用requests库发送HTTP请求,获取搜房网的网页源代码。 3. 使用BeautifulSoup4库解析网页源代码,提取所需数据,比如二手房的价格、面积、地点等。 4. 使用MySQLdb库连接MySQL数据库,并将获取到的数据保存到数据库中。 通过以上步骤,你就可以使用Python爬虫搜房网上获取二手房数据,并将其保存到MySQL数据库中,以便进行进一步的分析和应用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【Python爬虫系列】Python 爬取搜房网二手房数据](https://blog.youkuaiyun.com/yuzhucu/article/details/54135183)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [scrapy 爬虫实战----爬取搜房网十六城市房产数据](https://blog.youkuaiyun.com/San_Junipero/article/details/80724035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值