Python爬取房天下二手房信息

实现python爬取房天下网站所有城市的二手房信息


一、相关知识

import csv
with open("11.csv","w") as csvfile: 
    writer = csv.writer(csvfile)
    writer.writerow(["a","b","c"])
    writer.writerows([[1,1,1],[2,2,2],[3,3,3]])

二、目标

要求爬取房天下各大城市的二手房信息(www.fang.com)
需爬取所有城市的二手房信息,并存在csv文件中,可以所有数据放在一个文件中,但要记录是哪个省,哪个城市。也可以每个城市的数据放在一个csv文件中。要求爬取每个房源信息包括标题、面积、价格、地址等信息。

三、实现思路

1.准备工作

获取网址并解析

  • 分析房天下各城市各页的网址,得出大部分城市某页的网址为 https://城市.esf.fang.com/?i=30+页数
  • 解析网页为文本:
def response(url, headers):
    html = requests.get(url=url, headers=headers)
    html.encoding = html.apparent_encoding
    return html.text

2.获取所有城市及对应网址

解析该网页代码,获取各城市名及链接,并存到列表
在这里插入图片描述

3.遍历城市,获取所需信息

  • 先获取每个城市的页数,然后每个城市每页依次解析
  • 通过f12查看网页源代码,分析所需信息

4.将分解的信息存到csv中

四、完整代码

# -*- coding:utf-8 -*-
import requests
from lxml import etree
import re
import csv
from bs4 import BeautifulSoup
from pyasn1.compat.octets import null

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


def response(url, headers):
    html = requests.get(url=url, headers=headers)
    html.encoding = html.apparent_encoding
    return html.text


def crawl(url, write, headers):
    html = response(url,headers)
    soup = BeautifulSoup(html, 'lxml')
    titles = []  # 存放所有房源标题的列表
    house_types = []  # 存放所有房源房型的列表
    sizes = []  # 存放所有房源面积的列表
    floors = []  # 存放所有房源楼层的列表
    orientations = []  # 存放所有房源朝向的列表
    addrs = []  # 存放所有房源地址的列表
    totals = []  # 存放所有房源总价的列表
    prices = []  # 存放所有房源单价的列表

    items1 = soup.find_all('span', class_="tit_shop")
    for item in items1:
        titles.append(item.string.split()[0])

    items2 = soup.find_all('p', class_="tel_shop")
    for item in items2:
        house_types.append(item.contents[0].split()[0])
        sizes.append(item.contents[2].split()[0])
        floors.append(item.contents[4].split()[0])
        orientations.append(item.contents[6].split()[0])


    items4 = soup.find_all('p', class_="add_shop")
    for item in items4:
        addrs.append(item.contents[3].string)

    items5 = soup.find_all('dd', class_="price_right")
    for item in items5:
        totals.append(item.contents[1].contents[1].string)
        prices.append(item.contents[3].string)

    for i in range(len(titles)):
        write.writerow([titles[i], house_types[i], sizes[i], floors[i], orientations[i], addrs[i], totals[i],prices[i]])


def crawlCity(url2,headers,address_list,hrefs):
    html2 = response(url2,headers)
    soup = BeautifulSoup(html2, 'lxml')
    items = soup.find_all('a', class_="red")
    for item in items:
        address_list.append(item.string)
        hrefs.append(item['href'])

def crawlPage(url,headers):
    html = response(url, headers)
    items = re.findall("共(.*)页",html)
    if(len(items)==0):
        return 0
    else:
        for item in items:
            return item


def main():
    totalpage = 0
    address_list = []
    hrefs = []
    url2 = 'https://gz.esf.fang.com/newsecond/esfcities.aspx'
    crawlCity(url2, headers,address_list,hrefs)

    key = ['标题', '户型', '面积', '楼层', '朝向', '地址', '总价/万', '单位价格']  # ,'总价','单位价格']
    for i in range(len(address_list)):
        with open('{}.csv'.format(address_list[i]), 'a', newline='', encoding='utf-8') as fp:
            write = csv.writer(fp)
            write.writerow(key)
            print('现在爬取%s的二手房信息' % address_list[i])
            pageurl = "http:"+hrefs[i]
            if(crawlPage(pageurl,headers)==0):
                print("该城市无房源信息\n")
                continue
            else:
                totalpage=int(crawlPage(pageurl,headers))

                for page in range(1, totalpage+1):
                    pages = (str)(page + 30)
                    new_url = "http:"+hrefs[i]+"/?i="+pages
                    crawl(new_url, write, headers)
                    print('第%s页爬取完成' % page)
                print('已完成%s爬取' % address_list[i])
                print('\n')



if __name__ == '__main__':
    main()


五、实现结果

在这里插入图片描述


编程小白,有错误请大佬指出…

本人原创,欢迎转载

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值