將您使用BeautifulSoup抓取的數據移動到CSV文件中似乎至關重要。我接近成功,但不知何故CSV文件中的每一列都是來自刮取信息的一個字母,並且它只是移動最後一個項目刮擦。使用BeautifulSoup將刮取的數據移動到csv
這裏是我的代碼:
import urllib2
import csv
from bs4 import BeautifulSoup
url = "http://www.chicagoreader.com/chicago/BestOf?category=4053660&year=2013"
page = urllib2.urlopen(url)
soup_package = BeautifulSoup(page)
page.close()
#find everything in the div class="bestOfItem). This works.
all_categories = soup_package.findAll("div",class_="bestOfItem")
print(winner_category) #print out all winner categories to see if working
#grab just the text in a tag:
for match_categories in all_categories:
winner_category = match_categories.a.string
#Move to csv file:
f = file("file.csv", 'a')
csv_writer = csv.writer(f)
csv_writer.writerow(winner_category)
print("Check your dropbox for file")
Thx。任何關於我可以在哪裏學習這個細節的建議。我在谷歌搜索等方面拼湊在一起。 – user1922698
@ user1922698好吧,有多種資源。 [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/bs4/doc/)文檔,[csv](https://docs.python.org/2/library/csv.html)模塊文檔。另外,請在您感興趣的標籤中查看SO中最常見的問題。希望有所幫助。 – alecxe