This is my first time using Python 2.7, I do like it. However, I am trying to figure out how I can put extracted data from the URL into a CSV file. I found this tutorial, but when I run my script:
# import libraries
import csv
import urllib2
from bs4 import BeautifulSoup
# specify the url
quote_page = 'http://www.bkfrem.dk/default.asp?id=19'
# query the website and return the html to the variable ‘page’
page = urllib2.urlopen(quote_page)
# parse the html using beautiful soup and store in variable soup
soup = BeautifulSoup(page, 'html.parser')
# create CSV file
csvfile = csv.writer(open('firsteam.csv', 'w'))
csvfile.writerow(["Name", "Position"])
# take out the <div> of name and get its value
items = soup.find_all('div', attrs={'class': 'visTruppenContainer'})
for i in range(len(items)):
playerInfo = items[i].getText(separator=u' ')
imageURL = items[1].find('img')['src']
csvfile.writerow([playerInfo, imageURL])
print (playerInfo)
print (imageURL)
I get this error:
Traceback (most recent call last): File "C:/Users/User/Desktop/script2.py", line 26, in <module> csvfile.writerow([playerInfo, imageURL]) UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 27: ordinal not in range(128)
What am I doing wrong? Do I have to convert the data before writing to the CSV file?
encode('utf-8')?csvfile.writerow([playerInfo, imageURL])(I can't try it because when I convert this to Python 3 it works) so I can't tell you where exactly but it is certainly in one of those two