last day (15 days later) » 

3:20 PM
1
A: Wrong accented characters using Beautiful Soup in Python on a local HTML file

αԋɱҽԃ αмєяιcαηfrom bs4 import BeautifulSoup with open("AH.html") as f: soup = BeautifulSoup(f, 'html.parser') tb = soup.find("table") for item in tb.find_all("tr")[55]: print(item.text) I've to say, that your first code is actually fine and should works. Regarding the second code, yo...

 
Thank you, however I get the same wrong output as you can see from here i.imgur.com/aH69dHM.png I guess the problem is my computer?
 
@soundwave read the full answer please.
 
thanks, this is the output i.imgur.com/Eu2fAZK.png
 
@soundwave well, check with with open("AH.html", encoding="utf-8") as f:
@soundwave I noticed that you have did faulty thing !! please and please. run your code outside Python interpreter !! for the import sys code . put it in a file and then run it as py code.py
 
Excuse me, what do you mean? If I run print(sys.getdefaultencoding()) and others commands outside Python I get errors. I saved the code that you wrote at the beginning of the answer, inside a file called score2.py, but when in the cmd I run both score2.py or py score2.py I got the wrong output
 
3:20 PM
@soundwave Hi, there?
Kindly create a file called check.py
insert the following code inside it
import sys

print(sys.getdefaultencoding())
print(sys.stdin.encoding)
print(sys.stdout.encoding)
print(sys.stderr.encoding)
then run python check.py
and return back the output for me.
 
hi, thank you for the support, now i will do it
 
You welcome. waiting for you.
 
C:\Users\...\Desktop>python check.py
utf-8
utf-8
utf-8
utf-8
 
well, now it's not a system issue.
let's move to the next step
 
okay what is it
 
3:24 PM
create another file called script.py
insert the following inside it
from bs4 import BeautifulSoup


with open("AH.html") as f:
    soup = BeautifulSoup(f, 'html.parser')
    tb = soup.find("table")
    for item in tb.find_all("tr")[55]:
        print(item.text)
and run it via python script.py
make sure that AH.html is within the same directory where you run the code.
 
C:\Users\...\Desktop>python script.py

2:22 - Il Destino È Già Scritto (2017 ITA/ENG) [1080p] [BLUWORLD]
yes they are in the same path
 
change open("AH.html") to open("AH.html", encoding="UTF-8")
 
C:\Users\...\Desktop>python script.py

2:22 - Il Destino È Già Scritto (2017 ITA/ENG) [1080p] [BLUWORLD]
 
now it's works?
 
Oh it works! Thank you so much!
You have been very kind to help
 
3:27 PM
you welcome. the issue that your system configuration isn't UTF-8 internally
is it windows 7 ?
 
I'm using windows 10, is there an easy way to check the system configuration?
 
by the way, i don't think is a big problem, isn't it?
 
open powershell
run the following code
[System.Text.Encoding]::Default
@soundwave it's will be a huge problem in case if you talk to online sites. such as web scraping
as you will always get wrong chars
 
ok i trust you, this is the first time i got this error though
now i open powershell
IsSingleByte : True
BodyName : iso-8859-1
EncodingName : Western European (Windows)
HeaderName : Windows-1252
WebName : Windows-1252
WindowsCodePage : 1252
IsBrowserDisplay : True
IsBrowserSave : True
IsMailNewsDisplay : True
IsMailNewsSave : True
EncoderFallback : System.Text.InternalEncoderBestFitFallback
DecoderFallback : System.Text.InternalDecoderBestFitFallback
IsReadOnly : True
CodePage : 1252
 
3:31 PM
:D now we got it
BodyName : iso-8859-1
HeaderName : Windows-1252
 
what's goin on
 
that's different than UTF-8
 
is it because my default language is english but i'm using two keyboards english/italian?
 
now it's not. hold on
Click Start, then Control Panel
Click Clock, Language and Region
Click Region
Click the Administrative tab
press on Change system locale
you see use unicode UTF-8
 
should i tick the box?
 
so the system will can decode it on your behalf
as E is not the same as È
 
ok, i ticked the box, now it asks to restart the computer
i will come back in a minute
thanks
 
take your time
 
here i am
i confirm that now it works even without "encoding='UTF-8'"
 
many thanks for your big help
 
feel free to accept my answer if it's helps you.
enjoy the rest of your day.
 
thank you so much, have a nice day ^^,
 
you welcome :P
 
how can i give you a bounty?
 

last day (15 days later) »