Python Beautiful Soup 'ascii' 编解码器无法编码字符 u'\xa5'

Python Beautiful Soup 'ascii' codec can't encode character u'\xa5'

我在网络抓取页面的某些元素时遇到了一些奇怪的字符。似乎出错的字符是:

? ????Á¢¢Á? /?? />? /??? ?/¢¥Á ??%% ?Á ?????Á? ?> /???¥??> ¥? ¥©Á ?>¢¥/%%/¥??> ? >Á? Â?Á ©???¢ ñ%Á?¥???/% Á%Á?¥??>?? />? Â??Á? ??¥?? ??¢¥????¥??> ¢`¢¥Á¢ ??%% ?Á ??À?/?Á? ¥? _ÁÁ¥ ?>??Á/¢?>À Á????Á>¥ ????¥Á? />? ??__?>??/¥??>¢ ?Á

我的代码如下

url= "http://www.nsf.gov#######@#@#@##";
    #webbrowser.open(url,new =new );
    flagcnt+=1
    if flagcnt%20==0: #autosleep for avoiding shut-out
        print "flagcount: "
        print flagcnt
        time.sleep(5)
     #Program Code extraction
    r = requests.get (url)
    sp=BeautifulSoup(r.content)

页数:http://www.nsf.gov/awardsearch

我阅读了有关此错误的所有页面,其中一些建议解码和编码,但它们似乎没有 help.I 不知道这里使用的是哪种编码。尝试降级 BS 版本但没有帮助。 任何帮助表示赞赏。 Python 2.7 BS 4

这对我有用:

page_text = r.text.encode('utf-8').decode('ascii', 'ignore')
page_soupy = BeautifulSoup.BeautifulSoup(page_text)