看板 Python 關於我們 聯絡資訊
<html> <body> <div class="section-content-row"> <h2 class="rt-goods-list-item-name"> <a class="rt-goods-list-item-name-link" href="http://goods.ruten.com.tw/item/show?21716971337096" title="前往Lowe Alpine Strike 24 運動背包/都會日用後背包 FDP55 黑商品頁面" target="_blank"> <span class="rt-goods-list-item-name-text" itemprop="name">Lowe Alpine Strike 24 運動背包/都會日用後背包 FDP55 黑</span> </a> </h2> <div class="rt-goods-list-item-price-sell"> <div class="rt-goods-list-item-price"> <strong class="item-price-symbol rt-text-larger rt-text-price" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">2,363<meta itemprop="price" content="2363"><meta itemprop="priceCurrency" content="TWD"> </strong> </div> </div> </div> </body> </html> 我想取出物品名稱 網址 價格 soup = BeautifulSoup(html_doc, 'html.parser') for each_div in soup.findAll('div',{'class':'section-content-row'}): urls = each_div.find_all('a',{'class':'rt-goods-list-item-name-link'}) url = [url['href'] for url in urls] print url titles = each_div.find_all('span',{'class':'rt-goods-list-item-name-text'}) title = [title.get_text() for title in titles] print(title) prices = each_div.find_all('strong', {'class' : 'rt-text-price'}) price = [price.get_text() for price in prices] print price 可是我程式碼這樣寫感覺好像不太好~不知道有沒有比較簡潔寫法? 下面是我的測試程式碼 https://gist.github.com/anonymous/f0fd45ee679e72efadaa979ab168c182 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 112.104.29.181 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1508090620.A.5F2.html
chuink: 商品名稱:each_div.h2.span.text 10/16 02:58
chuink: 商品網址:each_div.h2.a['href'] 10/16 02:58
chuink: 價格:each_div.div.div.strong.text 10/16 02:59