爬取租房網(wǎng)頁數(shù)據(jù)

#!/usr/bin/env python
#-*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup

# 性別不同,標(biāo)簽的class屬性內(nèi)容不同,通過這個差異區(qū)分房東性別
def get_lorder_sex(class_name):   
  if class_name == ['member_boy_ico']:        
      return '男'    
  elif class_name == ['member_girl_ico']:        
      return '女'

def get_links(url):    
  wb_data = requests.get(url)    
# 開始解析網(wǎng)頁數(shù)據(jù)    
soup = BeautifulSoup(wb_data.text, 'lxml')    
# 鼠標(biāo)放到圖片上,右鍵,審查元素,找到鏈接的css selector    
links = soup.select("#page_list > ul > li > a")       
#  由于鏈接有好多個,soup.select返回的是列表,需要用for一個個取出來    
for link in links:        
# 由于鏈接地址在標(biāo)簽的href屬性里面,所以要用get獲取        
    href = link.get("href")        
# 把得到的詳情頁鏈接,傳給函數(shù),這個函數(shù)可以得到詳細數(shù)據(jù)     
    get_detail_info(href)    

def get_detail_info(url):    
    wb_data = requests.get(url)    # 開始解析詳情頁數(shù)據(jù)    
    soup = BeautifulSoup(wb_data.text, 'lxml')   
 # 獲取名稱    
  titles = soup.select("body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > h4 > em")   
 # 獲取地址   
   addresss = soup.select("body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > p > span.pr5")   
 # 獲取價格    
prices = soup.select("#pricePart > div.day_l > span")    
# 獲取圖片    
images = soup.select("#curBigImage")    
# 獲取房東頭像    
avartars = soup.select("#floatRightBox > div.js_box.clearfix > div.member_pic > a > img")    
# 獲取房東姓名    
    names = soup.select("#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > a")    
# 獲取房東性別    
    sexs = soup.select("#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > span")    
  for title, address, price, image, avartar, name, sex 
in zip(titles, addresss, prices, images, avartars, names, sexs):        
# 從標(biāo)簽里面提取內(nèi)容        
data = {            
"title": title.get_text(),            
"address": address.get_text(),            
"price": price.get_text(),            
"image": image.get("src"),           
 "avartar": avartar.get("src"),           
 "name": name.get_text(),          
  "sex": get_lorder_sex(sex.get("class"))       
 }       
 print(data)
# 生成10個列表頁面地址
urls = ["http://bj.xiaozhu.com/search-duanzufang-p{}-0/"
.format(number) for number in range(1, 10)]
# 從鏈接列表中,用for一個個取出來
for single_url in urls:    
# 把得到的列表頁面鏈接,傳給函數(shù),這個函數(shù)可以得到詳情頁鏈接    get_links(single_url)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容