Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t转换为';字节';对象到str隐式HTML解析器Python3错误_Python_Python 3.x_Html Parsing_Python 3.4_Html Parser - Fatal编程技术网

Can';t转换为';字节';对象到str隐式HTML解析器Python3错误

Can';t转换为';字节';对象到str隐式HTML解析器Python3错误,python,python-3.x,html-parsing,python-3.4,html-parser,Python,Python 3.x,Html Parsing,Python 3.4,Html Parser,我试图在Macbook Air(OS X)上用Python 3.4.2创建一个HTML解析器: 明文.py: from html.parser import HTMLParser import urllib.request, formatter, sys website = urllib.request.urlopen("http://www.profmcmillan.com") data = website.read() website.close() format = formatter.

我试图在Macbook Air(OS X)上用Python 3.4.2创建一个HTML解析器:

明文.py:

from html.parser import HTMLParser
import urllib.request, formatter, sys

website = urllib.request.urlopen("http://www.profmcmillan.com")
data = website.read()
website.close()
format = formatter.AbstractFormatter(formatter.DumbWriter(sys.stdout))
ptext = HTMLParser(format)
ptext.feed(data)
ptext.close()
但我得到了以下错误:

Traceback (most recent call last):
  File "/Users/deannarobertazzi/Documents/plaintext.py", line 9, in <module>
    ptext.feed(data)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/html/parser.py", line 164, in feed
    self.rawdata = self.rawdata + data
TypeError: Can't convert 'bytes' object to str implicitly
回溯(最近一次呼叫最后一次):
文件“/Users/deannarobertazzi/Documents/plaintext.py”,第9行,在
ptext.feed(数据)
提要中的文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/html/parser.py”,第164行
self.rawdata=self.rawdata+data
TypeError:无法将“bytes”对象隐式转换为str

我查看了Python文档,很明显,在Python3中解析HTML数据的方式与在Python2中解析HTML数据的方式大不相同。我不知道如何修改代码以使其适用于Python3。多谢各位

2.x隐式转换仅在所有字节都在ascii范围内时有效。[0-127]

>>> u'a' + 'b'
u'ab'
>>> u'a' + '\xca'

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    u'a' + '\xca'
UnicodeDecodeError: 'ascii' codec can't decode byte 0xca in position 0: ordinal not in range(128)

因此,如果要添加非ascii字符,并且编码没有更改,
utf-8
将不起作用。注意字节的编码确实是无可替代的。如何发现或猜测网页的编码(假设只使用了一种编码)是一个单独的主题。

通过TestRing使用的
数据是什么编码?例如,如果
latin-1
,则
ptext.feed(data.decode('latin-1'))
将起作用。它与Python2没有太大区别,您现在必须仔细区分文本(Unicode字符字符串)和字节字符串(任意字节字符串,通常通过各种可能的编解码器对文本进行编码)。。。这一直是一个好主意,但现在是强制性的:-)。我将UTF-8包含在ptext.feed(data.decode('UTF-8')行中,它起了作用。
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">