Python 美化组找不到元素
我正试图写一个程序,提取以下网站的价格。我正在用selenium下载该站点,然后尝试用beautifulsoup或selenium本身对其进行解析 我确定我想要的信息始终是class=“totalPrice”,我想将它们全部提取出来,理想情况下作为列表Python 美化组找不到元素,python,web-scraping,beautifulsoup,html-parsing,Python,Web Scraping,Beautifulsoup,Html Parsing,我正试图写一个程序,提取以下网站的价格。我正在用selenium下载该站点,然后尝试用beautifulsoup或selenium本身对其进行解析 我确定我想要的信息始终是class=“totalPrice”,我想将它们全部提取出来,理想情况下作为列表 <td class="totalPrice" colspan="3"> Total: £560 <span class="sr_room_reinforcement"></span> </td>
<td class="totalPrice" colspan="3">
Total: £560
<span class="sr_room_reinforcement"></span>
</td>
首先,您需要等待何时加载总价。使用位于预期条件下的元素的前序的类
我还发现,您需要通过所需的功能覆盖浏览器的用户代理
来假装不是PhantomJS
完整的工作代码:
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as bs
url = 'http://www.booking.com/searchresults.en-gb.html?label=gen173nr-17CAEoggJCAlhYSDNiBW5vcmVmaFCIAQGYAS64AQTIAQTYAQHoAQH4AQs;sid=1a43e0952558ac0ad0061d5b6523a7bc;dcid=1;checkin_monthday=4;checkin_year_month=2016-2;checkout_monthday=11;checkout_year_month=2016-2;city=-2601889;class_interval=1;csflt=%7B%7D;group_adults=7;group_children=0;highlighted_hotels=1192837;hp_sbox=1;label_click=undef;no_rooms=1;review_score_group=empty;room1=A%2CA%2CA%2CA%2CA%2CA%2CA;sb_price_type=total;score_min=0;si=ai%2Cco%2Cci%2Cre%2Cdi;ss=London;ssafas=1;ssb=empty;ssne=London;ssne_untouched=London&;order=price_for_two'
# setting a custom User-Agent
user_agent = (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"
)
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = user_agent
driver = webdriver.PhantomJS(desired_capabilities=dcap)
driver.get(url)
# wait for the total prices to become present
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".totalPrice")))
content = driver.page_source
driver.close()
soup = bs(content, 'lxml')
for e in soup.select('.totalPrice'):
print(e.text.strip())
它打印:
Total: US$781
Total: US$814
Total: US$831
Total: US$864
Total: US$895
Total: US$914
Total: US$915
Total: US$967
Total: US$1,031
顺便说一句,你并不真的需要
BeautifulSoup
-你可以-它非常强大。以下是您如何查找总价的方法:
for price in driver.find_elements_by_css_selector(".totalPrice"):
print(price.text.strip())
for price in driver.find_elements_by_css_selector(".totalPrice"):
print(price.text.strip())