2015-11-19 98 views
1

我想写一个简单的网页刮脚本,所以我写了这段代码,我得到一个错误。Python web刮(请求,BeautifulSoup)

import requests 
from bs4 import BeautifulSoup 

r = requests.get('http://the website that I need.com') 

soup = BeautifulSoup(r.content) 

print(soup.prettify()) 

而且我得到一个错误说:

Traceback (most recent call last): 
    File "course.py", line 18, in <module> 
    print(soup.prettify()) 
    File "C:\Python34\lib\encodings\cp437.py", line 19, in encode 
    return codecs.charmap_encode(input,self.errors,encoding_map)[0] 
UnicodeEncodeError: 'charmap' codec can't encode character '\u203a' in position 
32558: character maps to <undefined> 

我使用Python 3.4.0

因此,谁能告诉到底是怎么回事?

+0

尝试使用'r.text'而不是'r.content'? –

+0

我试过,但它因此未工作 – Yya09

回答

-1

我相信这是一个编码问题:尝试在返回字符串添加一个编码类型:

Exmample编码为UTF-8 汤= BeautifulSoup(r.content.encode( 'UFT-8') )

+0

我试过,但它没有工作,那么说:回溯(最后最近一次调用): 文件“course.py”,10号线,在 汤= BeautifulSoup(r.content.encode( 'uft-8')) AttributeError:'字节'对象没有属性'编码' – Yya09

+0

我看到http://stackoverflow.com/questions/7219361/python-and-beautifulsoup-encoding-issues prettify方法编码字符集是可以设置参数ex: soup.prettify('utf-8') –