2016-03-07 89 views
0

這是我在那裏我試圖獲取數據flipkartIndexError:列表索引超出範圍,而使用BS4

鏈接和代碼的一部分:

<div class="toolbar-wrap line section"> 
    <div class="ratings-reviews-wrap"> 
     <div itemprop="aggregateRating" itemscope="" itemtype="http://schema.org/AggregateRating" class="ratings-reviews line omniture-field"> 
     <div class="ratings"> 
      <meta itemprop="ratingValue" content="1"> 
      <div class="fk-stars" title="1 stars"> 
       <span class="unfilled">★★★★★</span> 
       <span class="rating filled" style="width:20%"> 
       ★★★★★ 
       </span> 
      </div> 
      <div class="count"> 
       <span itemprop="ratingCount">2</span> 
      </div> 
     </div> 
     </div> 

    </div> 

</div> 

這裏我要取從1星title= 1 star和2從<span itemprop="ratingCount">2</span>

我試試下面的代碼

x = link_soup.find_all("div",class_='fk-stars')[0].get('title') 

print x, " product_star" 
y = link_soup.find_all("span",itemprop="ratingCount")[0].string.strip() 
print y 

但它給

IndexError: list index out of range

+1

您的代碼工作正常,我 – gtlambert

+0

這就是我試圖湯http://www.flipkart.com/kanika-women-s-nighty/p/itmed2yqzdgqgz3q?pid=NDNED2YQU9HPESEA&al=FvCq1Xh5FyMN11cjuYgL6MldugMWZuE7svdkPtTGUY6%2F3Fk9x0T1uX3Tym%鏈接2F%2F6pAalZzKUG0RUT0%3D&ref = L%3A9146140901050108837&srno = p_14&query = nighty&otracker = from-search – YAM

+0

對我來說也適用。有沒有涉及的藏品?也許你正在迭代一個div列表? – siphr

回答

0

您在瀏覽器中看到的內容實際上不存在在從this URL檢索到的原始HTML。

當用瀏覽器加載時,頁面會執行AJAX調用來加載額外的內容,然後將其動態插入到頁面中。其中一個電話會獲得您之後的收視率信息。具體來說,this URL是包含作爲「操作欄」插入的HTML的文件。

但是,如果您使用Python檢索主頁面,例如與requests,urllib et。 al。,動態內容未加載,這就是爲什麼BeautifulSoup無法找到標籤。

您可以分析主頁以查找實際鏈接,檢索該鏈接,然後通過BeautifulSoup運行它。該鏈接看起來像是以/p/pv1/spotList1/spot1/actionBar開頭,或者可能是actionBar就足以找到實際的鏈接。

或者您可以使用selenium加載頁面,然後獲取並處理呈現的HTML。

相關問題