0

i need to save BeautifulSoup results to .txt file. and i need convert results to string with str() and not worked because list is UTF-8 :

# -*- coding: utf-8 -*-

page_content = soup(page.content, "lxml")

links = page_content.select('h3', class_="LC20lb")

for link in links:
    with open("results.txt", 'a') as file:
        file.write(str(link) + "\n")

and get this error :

  File "C:\Users\omido\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 183-186: character maps to <undefined>

1 Answer 1

1

If you want to write to the file as UTF-8 as well, you’ll need to specify that:

with open("results.txt", 'a', encoding='utf-8') as file:
    file.write(str(link) + "\n")

and it’s a good idea to only open the file once:

with open("results.txt", 'a', encoding='utf-8') as file:
    for link in links:
        file.write(str(link) + "\n")

(You can also print(link, file=file).)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.