Python3项目:练习爬取租房信息
Python3项目:练习一爬取单一租房页面信息
-需要抓取的页面信息:
-抓取页面:http://bj.xiaozhu.com/search-duanzufang-p1-0/ (点击进去第一个租房信息)
-抓取效果:
-项目代码:
from bs4 import BeautifulSoup
import requests
import time
url = 'http://bj.xiaozhu.com/fangzi/3686435130.html'
html = requests.get(url)
soup = BeautifulSoup(html.text,'lxml')
title = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > h4 > em')
adr = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > p > span')
money = soup.select('div.day_l')
pic = soup.select('#curBigImage')
touxiang = soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > a > img')
name = soup.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > a')
gender = soup.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > span')
for title,adr,money,pic,touxiang,name,gender in zip(title,adr,money,pic,touxiang,name,gender):
data = {
'title' : title.get_text(),
'adr' : adr.get_text(),
'money' : money.get_text(),
'pic' : pic.get('src'),
'touxiang' : touxiang.get('src'),
'name' : name.get_text(),
'gender' : gender.get('class'), #关于房东性别,想通过“member_girl_ico”属性抓取,但观察发现有许多房东没有这个属性
}
print(data)