我正在尝试解析通过 pyCurl 检索到的 HTML 页面,但 pyCurl WRITEFUNCTION 将页面作为 BYTES 而不是字符串返回,因此我无法使用 BeautifulSoup 解析它。
有什么方法可以将 io.BytesIO 转换为 io.StringIO 吗?
或者有没有其他方法解析HTML页面?
我正在使用 Python 3.3.2。
最佳答案
接受的答案中的代码实际上完全从流中读取以进行解码。下面是正确的方法,将一个流转换为另一个流,其中可以逐 block 读取数据。
# Initialize a read buffer
input = io.BytesIO(
b'Inital value for read buffer with unicode characters ' +
'ÁÇÊ'.encode('utf-8')
)
wrapper = io.TextIOWrapper(input, encoding='utf-8')
# Read from the buffer
print(wrapper.read())
关于html - 将 io.BytesIO 转换为 io.StringIO 来解析 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24566630/