0

这是我试图获取数据的链接 Flipkart

和代码的一部分:

   <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">★★★★★&lt;/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:列表索引超出范围

4

1 回答 1

0

您在浏览器中看到的内容实际上并不存在于从此URL检索的原始 HTML 中。

当使用浏览器加载时,页面执行 AJAX 调用以加载其他内容,然后将其动态插入到页面中。其中一个电话会获取您所追求的收视率信息。具体来说,此 URL是包含作为“操作栏”插入的 HTML 的 URL。

但是,如果您使用 Python 检索主页,例如 withrequestsurllib。al.,动态内容没有加载,这就是 BeautifulSoup 找不到标签的原因。

您可以分析主页以找到实际链接,检索该链接,然后通过 BeautifulSoup 运行它。该链接看起来以这样开头/p/pv1/spotList1/spot1/actionBar,或者可能actionBar足以找到实际链接。

或者您可以使用selenium加载页面,然后抓取并处理呈现的 HTML。

于 2016-03-07T12:20:09.803 回答