私はPythonには初めてです。これはBeautifulsoupでの私の最初の練習用コードです。私はまだ特定のデータ抽出の問題に対する創造的な解決法を学んだことはありません。CSVテキスト抽出Beautifulsoup
このプログラムはうまく印刷されますが、CSVに抽出するのは困難です。それは最初の要素を取りますが、他のすべての要素は後ろに残します。空白、区切り文字、または初期テキストの後でコードの抽出を停止させる何かがあるかもしれないと推測できるだけですか?
私はCSV抽出を行ごとに各アイテムに発生させようとしていましたが、明らかにうんざりしていました。助けて頂ける、ありがとうございます。ここで
from urllib.request import urlopen
from bs4 import BeautifulSoup
import csv
price_page = 'http://www.harryrosen.com/footwear/c/boots'
page = urlopen(price_page)
soup = BeautifulSoup(page, 'html.parser')
product_data = soup.findAll('ul', attrs={'class': 'productInfo'})
for item in product_data:
brand_name=item.contents[1].text.strip()
shoe_type=item.contents[3].text.strip()
shoe_price = item.contents[5].text.strip()
print (brand_name)
print (shoe_type)
print (shoe_price)
with open('shoeprice.csv', 'w') as shoe_prices:
writer = csv.writer(shoe_prices)
writer.writerow([brand_name, shoe_type, shoe_price])
あなたはインデントに問題があります –