python - Moving scraped data using BeautifulSoup to csv -


it seems critical able move data you've scraped using beautifulsoup csv file. i'm close succeeding somehow each column in csv file 1 letter scraped info, , it's moving last item scrape.

here's code:

import urllib2 import csv 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 in div class="bestofitem). works. all_categories = soup_package.findall("div",class_="bestofitem") print(winner_category) #print out winner categories see if working  #grab text in tag: match_categories in all_categories:     winner_category = match_categories.a.string  #move csv file: f = file("file.csv", 'a') csv_writer = csv.writer(f) csv_writer.writerow(winner_category) print("check dropbox file") 

the problem writerow() expects iterable. in case receives string , splits individual characters. put each value list.

besides, need in loop.

also, can pass urllib2.urlopen(url) directly beautifulsoup constructor.

also, should use with context manager while working files.

here's code modifications:

import urllib2 import csv 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")  open("file.csv", 'w') f:     csv_writer = csv.writer(f)     match_categories in all_categories:         value = match_categories.a.string         if value:             csv_writer.writerow([value.encode('utf-8')]) 

the contents of file.csv after running script is:

best view performance space best amateur hip-hop dancer who's professional wrestler best dance venue in new digs best outré dance best (and vocal) mime best performance in fat suit best theatrical use of unruly facial hair ... 

besides, i'm not sure need csv module @ all.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -