用python爬虫获取安居客二手房网站柯桥区域的前十页的二手房标题、小区名称、单价、总价、户型、所在楼层、面积、装修情况、朝向、建成时间、有无电梯、所属区域、产权年限、产权性质、物业类型、小区均价、小区户数、物业费用、容积率、绿化率。
时间: 2023-10-27 10:08:11 浏览: 99
好的,我可以为您提供一些代码来帮助您实现这个功能。请先确保您已经安装了 Python 的 Requests 和 BeautifulSoup 库。
代码如下:
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
area = 'kq' # 柯桥区的简写
pages = range(1, 11) # 前十页的页码
for page in pages:
url = f'http://hz.anjuke.com/sale/{area}/p{page}/#filtersort'
html = requests.get(url, headers=headers).text
soup = BeautifulSoup(html, 'html.parser')
items = soup.select('.list-item')
for item in items:
title = item.select('.house-title > a')[0].text.strip()
community = item.select('.comm-name > a')[0].text.strip()
unit_price = item.select('.unit-price')[0].text.strip()
total_price = item.select('.price-det > .price')[0].text.strip()
room_type = item.select('.room-type')[0].text.strip()
floor = item.select('.floor')[0].text.strip()
area = item.select('.area')[0].text.strip()
decoration = item.select('.tags-bottom .item')[0].text.strip()
direction = item.select('.tags-bottom .item')[1].text.strip()
year = item.select('.tags-bottom .item')[2].text.strip()
elevator = item.select('.haskey-ex')[0].text.strip()
location = item.select('.house-details > .details-item')[0].text.strip()
ownership = item.select('.house-details > .details-item')[1].text.strip()
property_type = item.select('.house-details > .details-item')[2].text.strip()
avg_price = item.select('.brother-right')[0].select('.price-txt')[0].text.strip()
households = item.select('.house-details-list')[0].select('.list-col > .first')[0].text.strip()
property_fee = item.select('.house-details-list')[0].select('.list-col > .first')[1].text.strip()
volume_rate = item.select('.house-details-list')[0].select('.list-col > .first')[2].text.strip()
green_rate = item.select('.house-details-list')[0].select('.list-col > .first')[3].text.strip()
print(title, community, unit_price, total_price, room_type, floor, area, decoration, direction, year,
elevator, location, ownership, property_type, avg_price, households, property_fee, volume_rate,
green_rate)
这段代码将会爬取安居客二手房网站柯桥区域前 10 页的二手房信息,并输出每套房子的各项详细信息。
阅读全文