贝壳二手房爬虫

import requests
import re
from fake_useragent import UserAgent

url = 'https://bj.ke.com/ershoufang/'
headers = {
    'User-Agent': UserAgent().random
}
response = requests.get(url,headers=headers)
page = response.text

title = r'<a class="VIEWDATA CLICKDATA maidian-detail" title="(.*?)"'
title = re.findall(title,page)

houseInfo = r'(?s)<span class="houseIcon"></span>(.*?)</div>'
houseInfo = re.findall(houseInfo,page)

totalPrice = r'(?s)<div class="totalPrice">(.*?)</div>'
totalPrice = re.findall(totalPrice,page)

unitPrice = r'(?s)<div class="unitPrice" data-hid=".*?" data-price=".*?">(.*?)</div>'
unitPrice = re.findall(unitPrice,page)

for t,h,p,u in zip(title,houseInfo,totalPrice,unitPrice):
    print(t)
    print(h.strip().replace(' ','').replace('\n',''))
    print(p.strip().replace(' ','').replace('\n','').replace('<span>','').replace('</span>',''))
    print(u.strip().replace(' ','').replace('\n','').replace('<span>','').replace('</span>',''))
    print('-'*15)
上一篇:1.Java Spring MVC入门 安装


下一篇:刷题200. Number of Islands