前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理
本次目標
爬取Q房網資料
https://shenzhen.qfang.com/newhouse
爬取目標資料:
- 小區名字
- 售房狀態
- 房屋面積
- 戶型
- 開盤時間
- 交房時間
- 樓盤地址
- 售價
- 預計總價
emmmm,我看看就行了,買不起買不起
開發工具
- python 3.6.5
- pycharm
爬蟲代碼
匯入工具
import requests
import parsel
import csv
決議網頁,爬取資料
for page in range(1, 84):
print('===============================正在爬取第{}頁的資料================================================='.format(page))
url = 'https://shenzhen.qfang.com/newhouse/list/n{}'.format(page)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
selector = parsel.Selector(response.text)
lis = selector.css('.list-result li')
dit = {}
for li in lis:
title = li.css('.list-main-header a em::text').get() # 名字
dit['標題'] = title
status = li.css('.list-main-header i::text').get() # 是否在售
dit['房產狀態'] = status
space = li.css('.list-main div:nth-child(1) .space span::text').get() # 售房面積
dit['售房面積'] = space
type_list = li.css('.list-main.fl p:nth-child(3) span a::text').getall() # 戶型
type_str = '|'.join(type_list).strip().replace('\r\n', '').replace(' ', '') # 戶型
dit['戶型'] = type_str
kp_time = li.css('.new-house-info > div:nth-child(2) > p.space.fl.clearfix > span::text').get() # 開盤時間
dit['開盤時間'] = kp_time
cs_time = li.css('.new-house-info > div:nth-child(2) > p:nth-child(3)> span::text').get() # 出售時間
dit['出售時間'] = cs_time
address = li.css('.list-main a:nth-child(3)::text').get() # 地址
if not address == None:
address = address.strip()
else:
address = None
dit['地址'] = address
Price = li.css('.list-price .bigger .amount::text').get() # 售價
dit['售價'] = Price
hj_Price = li.css('.list-price .smaller::text').get() # 預計總價
dit['預計總價'] = hj_Price
保存資料
f = open('房產資料.csv', mode='a', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['標題', '房產狀態', '售房面積', '戶型', '開盤時間', '出售時間', '地址', '售價', '預計總價'])
csv_writer.writeheader()
print(dit)
運行代碼,效果如下圖
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/176522.html
標籤:Python
下一篇:Django筆記:視圖
