“如何修复”;UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\xa0';在第3656位:序号不在范围(128)内;Python中的错误

“如何修复”;UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\xa0';在第3656位:序号不在范围(128)内;Python中的错误,python,python-2.7,beautifulsoup,python-unicode,Python,Python 2.7,Beautifulsoup,Python Unicode,我正在编写一个代码,使用Beautifulsoup从学校网站抓取学生时间表。问题是我一直有这样一个UnicodeError:“ascii”编解码器无法对第3656位的字符u'\xa0'进行编码:序号不在范围(128)结果中,我无法解决它 import urllib2 from bs4 import BeautifulSoup import os def make_soup(url): thepage = urllib2.urlopen(url) soupdata = Beaut

我正在编写一个代码,使用Beautifulsoup从学校网站抓取学生时间表。问题是我一直有这样一个UnicodeError:“ascii”编解码器无法对第3656位的字符u'\xa0'进行编码:序号不在范围(128)结果中,我无法解决它

import urllib2
from bs4 import BeautifulSoup
import os

def make_soup(url):
    thepage = urllib2.urlopen(url)
    soupdata = BeautifulSoup(thepage, "html.parser")
    return soupdata

timetabledatasaved = ""
soup = make_soup("http://timetable.ait.ie/reporting/textspreadsheet;student+set;id;AL%5FKSWFT%5FR%5F5%0D%0A?t"
             "=student+set+textspreadsheet&days=1-5&weeks=21-32&periods="
             "3-20&template=student+set+textspreadsheet")

for record in soup.find_all('tr'):
    timetabledata = ""
    print record
    print '--------------------'
    for data in record('td'):
        timetabledata = timetabledata + "," + data.text
    if len(timetabledata) != 0:
        timetabledatasaved = timetabledatasaved + "\n" + timetabledata[1:]

#print timetabledatasaved

header = "Activity, Module, Type, Start, End, Duration, Weeks, Room, Staff, Student Groups"
file = open(os.path.expanduser("timetable.csv"), "wb")
file.write(bytes(header).encode("utf-8", errors="ignore"))
file.write(bytes(timetabledatasaved).encode("utf-8", errors="ignore"))
我使用了Utf-8,但在抓取时间表后,它仍然给我这个错误。我再次意识到,我的代码似乎甚至可以抓取页面中的Java脚本,但我只希望它打印出相关的时间表数据并将其保存为.csv文件

Traceback (most recent call last):
  File "/Users/tobenna/PycharmProjects/final_project/venv/timetable_scrape.py", line 30, in <module>
    file.write(bytes(timetabledatasaved).encode("utf-8", errors="ignore")) 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 3656: ordinal not in range(128)

Process finished with exit code 1
回溯(最近一次呼叫最后一次):
文件“/Users/tobenna/PycharmProjects/final_project/venv/timeline_scrap.py”,第30行,在
file.write(字节(timetabledatasaved).encode(“utf-8”,errors=“ignore”))
UnicodeEncodeError:“ascii”编解码器无法对位置3656处的字符u'\xa0'进行编码:序号不在范围内(128)
进程已完成,退出代码为1

Python 2中的
字节
str
的同义词,因此通过对值调用
字节()
,您将它们编码为ASCII码,而ASCII码不能处理
'\xa0'
这样的字符。直接对值进行编码:

file.write(header.encode("utf-8", errors="ignore"))
file.write(timetabledatasaved.encode("utf-8", errors="ignore"))