我是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])
您的缩进有挑战 –