Web Scraping NBA stats

Screen Link:

My Code:

import requests

for year in years:
    url = url_start.format(year)
    data = requests.get(url)
    
    with open("MVP/{}.html".format(year), "w+") as f:
        f.write(data.text)
Replace this line with your code

What I expected to happen:

What actually happened:

Replace this line with the output/error

UnicodeEncodeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13372/3323775649.py in
6
7 with open(“MVP/{}.html”.format(year), “w+”) as f:
----> 8 f.write(data.text)

~\anaconda3\lib\encodings\cp1252.py in encode(self, input, final)
17 class IncrementalEncoder(codecs.IncrementalEncoder):
18 def encode(self, input, final=False):
—> 19 return codecs.charmap_encode(input,self.errors,encoding_table)[0]
20
21 class IncrementalDecoder(codecs.IncrementalDecoder):

UnicodeEncodeError: ‘charmap’ codec can’t encode character ‘\u010d’ in position 181692: character maps to

Attempting to pull years 1991-2022 into my jupyter lab folder named “MVP”

Regards: @jdibatti

Try to see if this works :

import requests

for year in years:
    url = url_start.format(year)
    data = requests.get(url)
    
    with open("mvp/{}.html".format(year), "w+") as f:# abre el archivo en modo w si existe y sobre escribe
        f.write(data.text)

Tell me if something happend, please.

A&E.

Thanks for the reply. I received the same error when trying that snippet.

OK.

try this:

with open(fname, "w", encoding="utf-8") as f:
    f.write(html)

stackoverflow

Tell me something, let’s see if we have it solved.

A&E.

unfortunately, didnt work either

heres the fix:

with open("mvp/{}.html".format(year), "w+", encoding="utf-8") as f:
    f.write(data.text)

i needed to include encoding=“utf-8” so you were on the right track. thanks again for your help!

1 Like

Hey @jdibatti I’m glad it served to you :partying_face:

What I did was precisely to give you the way and the link from where I looked for it.

Precisely the grace on learning is in knowing how to adapt code from others in yours.

if you consider that the solution was there, give me a solved and I will be very thankfully. :wink:

A&E