2014-09-22 66 views
1

我試圖捕獲此頁面上的訪問次數,但python返回沒有文本的標籤。使用請求和BeautifulSoup - Python返回沒有文本的標籤

這就是我所做的。

import requests 
from bs4 import BeautifulSoup 

r = requests.get("http://www.kijiji.ca/v-2-bedroom-apartments-condos/city-of-halifax/clayton-park-west-condo-style-luxury-2-bed-den/1016364514") 
soup = BeautifulSoup(r.content) 
print soup.find_all("span",{"class":"ad-visits"}) 

回答

2

你想湊由JavaScript這樣beautfulsouprequests被填充的值不會在這種情況下工作。

你需要使用類似selenium來獲得輸出。

from bs4 import BeautifulSoup 
from selenium import webdriver 

driver = webdriver.Firefox() 
driver.get("http://www.kijiji.ca/v-2-bedroom-apartments-condos/city-of-halifax/clayton-park-west-condo-style-luxury-2-bed-den/1016364514") 
soup = BeautifulSoup(driver.page_source , 'html.parser') 
print soup.find_all("span",{"class":"ad-visits"}) 

Selenium將返回網頁源代碼渲染,然後你可以使用beautifulsoup來獲取值

[<span class="ad-visits">385</span>] 
+0

確實的Python必須打開Firefox的檢索infromation? – JohnT 2014-09-23 13:56:57

+0

你可以使用firefox的'無頭',或者你也可以使用硒的另一個webdriver,但你需要有一個環境,它會呈現JavaScript。 – dataisbeautiful 2014-09-23 14:00:35

+0

你是什麼意思無頭的?你的代碼如何改變 – JohnT 2014-09-24 01:06:51

相關問題