【问题标题】:Python Beautiful Soup 'ascii' codec can't encode character u'\xa5'Python Beautiful Soup 'ascii' 编解码器无法编码字符 u'\xa5'
【发布时间】:2015-06-23 16:04:12
【问题描述】:

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

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

我的相关代码如下

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

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

【问题讨论】:

    标签: python html web-scraping beautifulsoup bs4


    【解决方案1】:

    这对我有用:

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

    【讨论】:

    • 感谢您的回复。它在 page_soupy = BeautifulSoup(page_text) 'module' object is not callable 上说。任何想法:)
    • 啊,那你导入的是完整的模块,而不是函数。我会编辑答案。
    • 老兄!你摇滚。非常感谢。
    猜你喜欢
    • 2018-05-05
    • 2018-04-15
    • 1970-01-01
    • 2013-04-21
    • 2015-04-13
    • 2017-02-19
    • 2018-07-07
    • 2011-04-05
    相关资源
    最近更新 更多