“ascii”编解码器无法编码字符“BeautifulSoup错误”

2024-05-14 23:17:15 发布

您现在位置:Python中文网/ 问答频道 /正文

这里是Python新手。目前正在为一个歌词网站编写一个爬虫程序,我在试图解析HTML时遇到了这个问题。我在用beauthulsoup和requires。

现在的代码是(在所有导入之后):

def function(artist_name):
    temp = "https://www.lyrics.com/lyrics/"
    if ' ' in artist_name:
        artist_name = artist_name.replace(' ', '%20')
    page = r.get(temp + artist_name.lower()).content
    soup = bs(page, 'html.parser')
    return soup

当我尝试对此进行测试时,我总是得到以下错误:

^{pr2}$

我试着在soup行的末尾添加.encode('utf-8'),它消除了错误,但不允许我使用任何beauthoulGroup方法,因为它返回字节。

我查看了这里的其他帖子,并尝试了他们为类似错误提供的其他解决方案。关于Python和Unicode,我还有很多需要了解的地方,但是如果有人能提供帮助和指导,我将不胜感激。


Tags: name程序artist网站html错误page歌词

热门问题