python - BeautifulSoup 中的“'ascii' 编解码器无法编码字符”错误
问题描述
Python新手在这里。目前正在为一个歌词网站编写爬虫,我在尝试解析 HTML 时遇到了这个问题。我正在使用 BeautifulSoup 和请求。
现在的代码是(毕竟导入等等):
import requests as r
from bs4 import BeautifulSoup as bs
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
当我尝试对此进行测试时,我不断收到以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character '\xa0' in position 8767: ordinal not in range(128)
我尝试添加.encode('utf-8')
到soup
行尾,它消除了错误,但不允许我使用任何 BeautifulSoup 方法,因为它返回字节。
我查看了此处的其他帖子,并尝试了他们为类似错误提供的其他解决方案。关于 Python 和 Unicode,我还有很多需要了解的地方,但如果有人可以提供帮助并提供一些指导,我将不胜感激。
解决方案
推荐阅读
- visual-studio-code - Gitlens在代码块之前导致换行
- reactjs - 如何让这些组件按钮在正确的卡片组件中呈现?
- java - 即使设置为正确的serialVersionUID,JMS activemq 序列化错误本地类也不兼容
- python - TensorFlow GPU 无法识别 GPU dll
- java - 在每个按钮中获取时间和日期单击android
- html - 引导列未在行内对齐
- python - 使用嵌套字典中的多个值遍历多行以更新python中的数据框
- javascript - 运动物体之间的碰撞检测
- laravel - Laravel 哪里有
- django - 在 DJANGO REST 框架中缓存