1
votes

I'm running a pretty simple script in Python to get data from a URL:

import urllib2
from bs4 import BeautifulSoup

def get_data():

    response = urllib2.urlopen('http://www.p2016.org/photos15/summit/trump012415spt.html')
    html = BeautifulSoup(response, 'html.parser')
    text = html.get_text()
    return text


print get_data()

I keep getting this error message:

PS C:\Users\Ben\pythonlearning\markov_chain> python fetch_data.py
Traceback (most recent call last):
  File "fetch_data.py", line 11, in <module>
    print get_data()
  File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\xa9' in position 22825: character maps to <undefined>

I've tried:

  • running it without the print command, and I get no error
  • having someone on another computer run the exact same code, and it works.
1
I would edit the title of the question - as it works without the print statement, the problem is clearly not in importing BeautifulSoup. - DeepSpace
@DeepSpace I edited the title - Quentin Pradet

1 Answers

0
votes

The difference you encounter between this error and "having someone on another computer run the exact same code, and it works" are due to differing versions of the CPython interpreter.

Python 3 marks a significant backwards-incompatible change in language features, and that's why you get this error.

You can confirm the difference by running this command on both platforms:

python -c 'import sys; print(sys.version)'