使用BeautifulSoup编码/解码问题

时间:2018-05-27 22:11:28

标签: python beautifulsoup decode encode

我有以下词典。当我使用漂亮的汤文本打印出值时,它会正确地打印到屏幕上,但是当我将它存储在dict中时,我会遇到编码问题。想知道在存储值时我应该做什么。我试过.encode

{'location': 'Century College.', 'instructor': 'Forsman,\xa0Deanna', 'time': 'na\xa0-\xa0na', 'subject': 'HIST', 'id': '001538', 'seats remaning': '7', 'delivery method': 'Completely Online-Asynchronous', 'days': 'n/a', 'number': '1032', 'offered through': 'Century College.', 'credits': '4.0', 'size': 'Open', 'status': 'Off-Campus Credit.', 'dates': '08/27\xa0-\xa012/20', 'title': 'U.S. History II (Online)', 'building/room': 'Online Classes ONLINE', 'sec': '70', 'end date': '12/20/2018', 'enrolled': '27', 'start date': '8/27/2018'}

2 个答案:

答案 0 :(得分:2)

在转换为Python dict对象之前,请先尝试将对象转换为字符串。一种方法是通过ast.literal_eval

from ast import literal_eval

# replace with some_string = str(my_beautiful_soup_object)
some_string = str({'location': 'Century College.', 'instructor': 'Forsman,\xa0Deanna', 'time': 'na\xa0-\xa0na', 'subject': 'HIST', 'id': '001538', 'seats remaning': '7', 'delivery method': 'Completely Online-Asynchronous', 'days': 'n/a', 'number': '1032', 'offered through': 'Century College.', 'credits': '4.0', 'size': 'Open', 'status': 'Off-Campus Credit.', 'dates': '08/27\xa0-\xa012/20', 'title': 'U.S. History II (Online)', 'building/room': 'Online Classes ONLINE', 'sec': '70', 'end date': '12/20/2018', 'enrolled': '27', 'start date': '8/27/2018'})

d = literal_eval(str(some_string))

答案 1 :(得分:0)

在将字符串放入dict值之前,我最终对字符串进行了编码和解码。

.encode('ascii', 'ignore').decode()