The problem is that writerow()
expects an iterable. In your case it receives a string and splits it into individual characters. Put each value into the list.
Besides, you need to do this in the loop.
Also, you can pass urllib2.urlopen(url)
directly to the BeautifulSoup
constructor.
Also, you should use with
context manager while working with files.
Here's the code with modifications:
import urllib2
import csv
from bs4 import BeautifulSoup
url = "http://www.chicagoreader.com/chicago/BestOf?category=4053660&year=2013"
soup_package = BeautifulSoup(urllib2.urlopen(url))
all_categories = soup_package.find_all("div", class_="bestOfItem")
with open("file.csv", 'w') as f:
csv_writer = csv.writer(f)
for match_categories in all_categories:
value = match_categories.a.string
if value:
csv_writer.writerow([value.encode('utf-8')])
The contents of the file.csv
after running the script is:
Best View From a Performance Space
Best Amateur Hip-Hop Dancer Who's Also a Professional Wrestler
Best Dance Venue in New Digs
Best Outré Dance
Best (and Most Vocal) Mime
Best Performance in a Fat Suit
Best Theatrical Use of Unruly Facial Hair
...
Besides, I'm not sure that you need csv
module at all.