2015-01-04 3 views
1

this URLBeautifulSoup으로 긁고 있습니다.원하는 DIV를 긁을 수 없습니다. - BeautifulSoup

나는 우리의 특징 제목 모든 DIV를 긁어하려면 :

if hotel_meetings_soup.select("div#contentArea div.highlightBox"): 
    print(hotel_meetings_soup.select("div#contentArea")) # debug 1 
    exit(0) 
    for meeting in hotel_meetings_soup.select("div#contentArea div.highlightBox"): 
     print("\n Feature start here\n") 
     print(meeting) 
     # Rest of code 

모든 div의가이 같은 클래스 highlightBox을하지만 난 잘 모릅니다 왜 debug 1 인쇄

이있는 DIV의 마크 업
Number Of Guest Rooms: 500 
Number Of Meeting Spaces: 29 
Largest Meeting Space: 17,377 sq ft (1,614.28 sq.m) 

(그 중 일부는 아니지만)

답변

0

아이디어는, 첫째, 텍스트하여 Our Featuresh3 요소 을 찾을 수 있습니다, 다음 find_next_siblings()을 사용하여 적절한 다음 형제를 찾을 수 :

import requests 
from bs4 import BeautifulSoup 

url = 'http://www.starwoodhotels.com/sheraton/property/meetings/index.html?language=en_US&propertyID=1391' 
response = requests.get(url, headers={ 
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' 
}) 

soup = BeautifulSoup(response.content) 
features = soup.find(text='Our Features') 

for div in features.parent.find_next_siblings('div', class_='highlightBox'): 
    print(div.text.strip()) 

인쇄를 :

Weddings 
Host a beautiful wedding in the Valley of the Sun with our spectacular views, lush ceremony lawns, and upscale ballrooms with pre-function space. Stellar catering and superb service ensure a amazing day. More > 
... 
Get Rewarded 
Earn Starpoints® and eligible nights toward SPG elite status on your next meeting or event. More > 
+0

을 나는 거를 제공하고있다 시도. .. 그러나 그것이 내가 다른 많은 호텔을 위해 thise DIVs를 긁을 수있는 이상한 is not 예를 들면. http://www.starwoodhotels.com/sheraton/property/meetings/index.html?propertyID=115 내가 내 질문에 게시 한 코드를 사용하여 필요한 모든 DIV를 얻을 수 있지만이 DIV가 내부에 있어도이 호텔에는 사용할 수 없습니다. 'contentArea' –

관련 문제