在尝试了我在这里找到的所有答案后,我仍然感到困惑。请建议如何去做。 print soup正在运行但属性错误不会发生。
from selenium import webdriver
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf8')
url = 'https://www.airbnb.com/s/New-York--NY--United-States/all?checkin=2017-11-05&checkout=2017-11-10&allow_override%5B%5D=&s_tag=fE7Oc2kI'
driver = webdriver.PhantomJS(executable_path = r'C:\Users\Vaibhav\Downloads\phantomjs\phantomjs-2.1.1-windows-bin-phantomjs.exe')
driver.get(url)
soup = BeautifulSoup(driver.page_source, "html5lib")
#print soup
div1 = soup.find('div', class_ = 'cardGrid_o1rkka')
for div2 in div1.find_all('div', class_ = 'container_j4k9t6-o_O-column-sm-1_zdxht7-o_O-column-md-2_3504zm-o_O-column-lg-2_14svpwf-o_O-column-xl-3_19vi8ax'):
title_span = div2.find('span', class_ = 'text_5mbkop-o_O-size_small_1gg2mc-o_O-weight_bold_153t78d-o_O-inline_g86r3e')
print title_span.text
price_span = div2.find('span', class_ = 'accessible_1m8bb6v')
print price_span.text
if 'reviews' in div2.text:
review_span = div2.find_all('span')[len(div2.find_all('span')) - 1]
print review_span.text
driver.quit()
这就是错误:
Traceback (most recent call last):
File "C:\Users\Vaibhav\Desktop\Python Course\Airbnbscrap\airbnbpart1.py", line 31, in <module>
for div2 in div1.find_all('div', class_ = 'container_j4k9t6-o_O-column-sm-1_zdxht7-o_O-column-md-2_3504zm-o_O-column-lg-2_14svpwf-o_O-column-xl-3_19vi8ax'):
AttributeError: 'NoneType' object has no attribute 'find_all'