其他分享
首页 > 其他分享> > 贝壳二手房爬虫

贝壳二手房爬虫

作者:互联网

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)

标签:totalPrice,贝壳,re,爬虫,replace,二手房,print,unitPrice,page
来源: https://blog.csdn.net/zhangju414/article/details/113796079