python爬取链家租房之获取房屋页面的详细信息(房名,地址,房
2017-06-20 本文已影响30人
宁静消失何如
__author__ = 'Lee'
from bs4 import BeautifulSoup
import requests
import time
url = 'https://bj.lianjia.com/zufang/101101613377.html'
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.36',
}
web_data = requests.get(url,headers=headers)
soup = BeautifulSoup(web_data.text,'lxml')
title = soup.title.text #房名
address = soup.select('div.zf-room > p > a')[0].text #地址
price = soup.select(' div.price > span.total')[0].text + '元'
area = (soup.select('div.zf-room > p ')[0].text).split(':')[-1]
home_url = url
print({'title':title ,
'address':address,
'price':price,
'area':area,
'home_url':home_url
})