3
votes

I've got an error message from python. It's a code that extracts phonetic symbols from the oxford online dictionary.

Traceback (most recent call last): File "C:\Download\Oxford_PhoneticSymbol\Oxford_PhoneticSymbol.py", line 24, in fw.write((result + "\n").encode("utf-8")) TypeError: write() argument must be str, not bytes

Original source is from; https://my.oschina.net/sfshine/blog/3076588

# -*- coding: UTF-8 -*-
import requests
import time
from bs4 import BeautifulSoup
f = open('./words.txt')
fw = open('./result.txt','a')

line = f.readline()
index = 0
while line:
    index = index+1
    url = "https://www.oxfordlearnersdictionaries.com/definition/english/" + line.strip()
    print(str(index) + ":" + url)
    wbdata = requests.get(url,headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36'}).text
    soup = BeautifulSoup(wbdata,'html.parser')
    news_titles = soup.select("span.pron-g > span.phon")
    # print(news_titles)
    result = ''
    for n in news_titles:   
        title = n.get_text()    
        if 'NAmE' in title:
            result += '['+title.replace('NAmE','').replace('//','') + ']'
    print(result)  
    fw.write((result + "\n").encode("utf-8"))
    line = f.readline()
    time.sleep(0.1)

fw.close()
f.close()

Like it needed, I have put two txt files in the same directory. And I filled a few words in words.txt and made the result.txt as blank, but I got the error.

(words.txt, saved as UTF-8)
source
technic
resource
power
box
created
computer
charged
learned

Any Idea how to fix this?

1
have you tried to change your open, fw = open('./result.txt','a') from 'a' to 'ab' - Christian Sloper
What did you think you did when you wrote (result + "\n").encode("utf-8")? The error message is very clear about what's going on, are you familiar with bytes vs strings? edit: scratch that, i see that you're copying a tutorial, that is giving a pretty sad example of how people mix up bytes and strings. - Paritosh Singh
@ChristianSloper I have just tried to change a to ab, and it worked, but missed few. thanks for your advice. *source (none) / technic (none, it doesn't exist on oxford) / resource [ˈriːsɔːrs][rɪˈsɔːrs] / power [ˈpaʊər] / box [bɑːks] / created (none) / computer [kəmˈpjuːtər] / charged [tʃɑːrdʒd] / learned [ˈlɜːrnɪd][lɜːrnd] - user12462771
You have a link in this question which is misleading. It currently displays one URL, but actually links to another URL. Please edit your question so the URL which is displayed is the same as the URL the link actually links to. This is probably just a formatting error, but it's not clear which URL you are actually wanting to be linking to, so I didn't just repair it. - Makyen♦
Ooops, thanks for comment. Link has been fixed. - user12462771

1 Answers

5
votes

The solution to this error when saving a downloaded page to file:

TypeError: write() argument must be str, not bytes

is:

fw = open('./result.txt','wb')

"b" for binary makes the difference.