2017-02-27 171 views
3

我試圖創建一個Python腳本來使用BeautifulSoup從tcgplayer.com中提取Yugioh卡價格的價格。當您在此網站上搜索卡時,它會返回一個包含來自不同賣家的多個價格的搜索結果頁面。我的目標是拉出所有這些價格。在下面的例子中,我打開搜索結果的一個名爲「A」細胞育種設備卡:Python BeautifulSoup返回空列表

import urllib2 
from bs4 import BeautifulSoup 
html = urllib2.open('http://shop.tcgplayer.com/productcatalog/product/show?newSearch=false&ProductType=All&IsProductNameExact=false&ProductName=%22A%22%20Cell%20Breeding%20Device') 
soup = BeautifulSoup(html, 'lxml') 
soup.find_all('span', {'class': 'scActualPrice largetext pricegreen'}) 

前幾天,運行soup.find_all線正確地給了我所需要的信息。然而,現在運行這個給我一個空數組[]。我已經非常廣泛地搜索了BeautifulSoup返回一個空數組,但我不確定它們是否適用於我,因爲它幾天前工作得很好。有人能幫助我指出正確的方向嗎?先謝謝你!

回答

2

您應該使用selenium使用真正的瀏覽器報廢:

from selenium import webdriver 

driver = webdriver.Chrome('/path/to/chromedriver') 
driver.get('http://shop.tcgplayer.com/productcatalog/product/show?newSearch=false&ProductType=All&IsProductNameExact=false&ProductName=%22A%22%20Cell%20Breeding%20Device') 
prices = driver.find_elements_by_css_selector('.scActualPrice') 
for element in prices: 
    print(element.text) 
driver.quit() 
+0

這工作完美。謝謝! –

0

本網站使用名爲Incapsula的服務。網站開發人員配置Incapsula以防止漫遊器訪問它的內容。

我建議你聯繫他們的管理員並申請訪問權限或向他們詢問API。

+0

使用硒爲我工作,但你認爲它會停在幾天的工作呢? –

+0

有了硒,你實際上是在打開瀏覽器並進行所有的操作,所以現在應該沒問題,但可能將來會有機會。 –

+0

使用硒不可靠 –