2016-05-17 109 views
1

在这里需要一些帮助。我试图打印出文字“Whirlpool®18 cu.Ft. Top Freezer Refrigerator - WRT148FZDB”,但没有找到find_all语句中的正确结构。我目前正在调用find_all作为下面的代码,但我知道这是错误的。python 3.5 find_all语句

g6_data = soup.find_all("div", {"class": "product_name"}) 

以下代码是我试图从中剔除信息的。

任何帮助表示赞赏。由于

<div class="product_info"> 

     <div id="product_name_7267017" class="product_name"> 

      <a aria-hidden="true" tabindex="-1" id="CatalogEntryViewDetailsLink_7267017" href="http://www.sears.ca/product/whirlpool-18-cuft-top-freezer-refrigerator-wrt148fzdb/646-000153197-WRT148FZDB"><span itemprop="name">Whirlpool® 18 cu.Ft. Top Freezer Refrigerator - WRT148FZDB</span></a> 
      <input type="hidden" id="ProductInfoName_7267017" value="Whirlpool® 18 cu.Ft. Top Freezer Refrigerator - WRT148FZDB"> 
     </div> 
+1

为什么是错误的?当你尝试这个时会发生什么? –

+0

Nitpicking,但这是一种方法(一种函数),而不是一个声明。 – Jules

+0

你能给出一个链接到你从中获取html的页面吗?我知道这是在西尔斯的某个地方,但究竟是什么页面 – Keatinge

回答

0

你需要拉的div内从锚标签的文本,你可以使用一个CSS选择器:

import requests 
from bs4 import BeautifulSoup 

soup = BeautifulSoup(requests.get("http://www.sears.ca/catalog/appliances-fridges-freezers-refrigerators-top-freezer-en-wp-836#facet:&productBeginIndex:0&orderBy:&pageView:grid&minPrice:&maxPrice:&pageSize:100&").text) 


for a in soup.select("div.product_name a"): 
    print(a.text) 
+0

谢谢,这就是炒作。刚才我必须用另一种方法来确定格式 – nobb666