Web サイトから 41 のアイテムとその価格のリストをスクレイピングしようとしています。しかし、私の出力 csv には、ページの最後にある 2 ~ 3 個の項目がありません。この理由として、一部のデバイスでは、他のデバイスとは異なるクラスで価格が記載されています。私のコードでの再帰は、名前と価格を一緒に実行しており、価格が別のクラスで言及されているアイテムについては、次のデバイスから価格値を取得しています。したがって、これらのデバイスの価格は以前のデバイスの再帰にすでに入力されているため、最後の 2 ~ 3 項目をスキップしています。以下は参照されたコードです:
# -*- coding: cp1252 -*-
import csv
import urllib2
import sys
import time
from bs4 import BeautifulSoup
page = urllib2.urlopen('http://www.att.com/shop/wireless/devices/smartphones.deviceListGridView.xhr.flowtype-NEW.deviceGroupType-Cellphone.paymentType-postpaid.packageType-undefined.html?taxoStyle=SMARTPHONES&showMoreListSize=1000').read()
soup = BeautifulSoup(page)
soup.prettify()
with open('AT&T_2012-12-28.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',')
spamwriter.writerow(["Date","Month","Day of Week","Device Name","Price"])
items = soup.findAll('a', {"class": "clickStreamSingleItem"},text=True)
prices = soup.findAll('div', {"class": "listGrid-price"})
for item, price in zip(items, prices):
textcontent = u' '.join(price.stripped_strings)
if textcontent:
spamwriter.writerow([time.strftime("%Y-%m-%d"),time.strftime("%B"),time.strftime("%A") ,unicode(item.string).encode('utf8').replace('™','').replace('®','').strip(),textcontent])
価格は通常下に記載されてlistGrid-price
いますが、現在価格が不足している 2 ~ 3 個のアイテムについてはlistGrid-price-outOfStock
、これも再帰に含める必要があります。これにより、アイテムの前に適切な価格が表示され、すべてのデバイスに対してループが実行されます。
プログラミング初心者のため、無知なことをお許しください