Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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
Python 通过requests.get from html页面从lxml.html.fromstring获取正确的UTF-8?_Python_Utf 8 - Fatal编程技术网

Python 通过requests.get from html页面从lxml.html.fromstring获取正确的UTF-8?

Python 通过requests.get from html页面从lxml.html.fromstring获取正确的UTF-8?,python,utf-8,Python,Utf 8,这是MWE,test.py——作为mypage内联编写的测试网页,由提供服务,因此您应该能够按原样运行此脚本: #/usr/bin/python #-*-编码:utf-8-*- 导入操作系统,系统 进口稀土 将lxml.html作为LH导入 导入请求 如果sys.version\u info[0]则根本问题是您使用的是confpage.content而不是confpage.text requests.Response.content为您提供从电线上拔下的原始字节(bytes在3.x中,str在

这是MWE,
test.py
——作为
mypage
内联编写的测试网页,由提供服务,因此您应该能够按原样运行此脚本:

#/usr/bin/python
#-*-编码:utf-8-*-
导入操作系统,系统
进口稀土
将lxml.html作为LH导入
导入请求

如果sys.version\u info[0]则根本问题是您使用的是
confpage.content
而不是
confpage.text

  • requests.Response.content
    为您提供从电线上拔下的原始字节(
    bytes
    在3.x中,
    str
    在2.x中)。编码是什么并不重要,因为您没有使用它
  • requests.Response.text
    根据
    编码提供解码的Unicode(
    str
    在3.x中,
    Unicode
    在2.x中)
因此,设置
编码
,然后使用
内容
没有任何作用。如果您只是将代码的其余部分更改为使用
文本
而不是
内容
(并消除Python 3中现在虚假的
解码
),它将起作用:

mystr = confpage.text
for line in iter(mystr.splitlines()):
  if 'Testing' in line:
    print(line)
confpagetree = LH.fromstring(confpage.text)
print(confpagetree) # <Element html at 0x7f4b7074eec0>
#print(confpagetree.text_content())
for line in iter(confpagetree.text_content().splitlines()):
  if 'Testing' in line:
    print(line)
mystr=confpage.text
对于iter中的线路(mystr.splitlines()):
如果“测试”在线路中:
打印(行)
confpagetree=LH.fromstring(confpage.text)
打印(confpagetree)#
#打印(confpagetree.text\u content())
对于iter中的行(confpagetree.text_content().splitlines()):
如果“测试”在线路中:
打印(行)

如果您想通过每个示例了解确切的问题:

  • 您的第一个示例在Python3中是正确的,但不是最好的方法。通过在
    内容上调用
    decode(“utf-8”)
    ,由于字节恰好是utf-8,因此可以正确地对它们进行解码。所以他们会正确地打印出来
  • 在Python2中,您的第一个示例是错误的。您只需打印
    内容
    ,这是一堆UTF-8字节。如果您的控制台是UTF-8(就像macOS上的一样,也可能是Linux上的一样),这将正常工作。如果您的控制台是其他的东西,比如cp1252或Latin-1(它在Windows上,也可能在Linux上),这将为您提供mojibake
  • 你的第二个例子也是错误的。通过将字节传递给
    LH.fromstring
    ,您迫使lxml猜测要使用的编码,它猜测拉丁语-1,因此您得到了mojibake

根本问题是您使用的是
confpage.content
而不是
confpage.text

  • requests.Response.content
    为您提供从电线上拔下的原始字节(
    bytes
    在3.x中,
    str
    在2.x中)。编码是什么并不重要,因为您没有使用它
  • requests.Response.text
    根据
    编码提供解码的Unicode(
    str
    在3.x中,
    Unicode
    在2.x中)
因此,设置
编码
,然后使用
内容
没有任何作用。如果您只是将代码的其余部分更改为使用
文本
而不是
内容
(并消除Python 3中现在虚假的
解码
),它将起作用:

mystr = confpage.text
for line in iter(mystr.splitlines()):
  if 'Testing' in line:
    print(line)
confpagetree = LH.fromstring(confpage.text)
print(confpagetree) # <Element html at 0x7f4b7074eec0>
#print(confpagetree.text_content())
for line in iter(confpagetree.text_content().splitlines()):
  if 'Testing' in line:
    print(line)
mystr=confpage.text
对于iter中的线路(mystr.splitlines()):
如果“测试”在线路中:
打印(行)
confpagetree=LH.fromstring(confpage.text)
打印(confpagetree)#
#打印(confpagetree.text\u content())
对于iter中的行(confpagetree.text_content().splitlines()):
如果“测试”在线路中:
打印(行)

如果您想通过每个示例了解确切的问题:

  • 您的第一个示例在Python3中是正确的,但不是最好的方法。通过在
    内容上调用
    decode(“utf-8”)
    ,由于字节恰好是utf-8,因此可以正确地对它们进行解码。所以他们会正确地打印出来
  • 在Python2中,您的第一个示例是错误的。您只需打印
    内容
    ,这是一堆UTF-8字节。如果您的控制台是UTF-8(就像macOS上的一样,也可能是Linux上的一样),这将正常工作。如果您的控制台是其他的东西,比如cp1252或Latin-1(它在Windows上,也可能在Linux上),这将为您提供mojibake
  • 你的第二个例子也是错误的。通过将字节传递给
    LH.fromstring
    ,您迫使lxml猜测要使用的编码,它猜测拉丁语-1,因此您得到了mojibake

太棒了-非常感谢,@abarnert;我总是对Python2和Python3中的哪个是哪个感到困惑,所以您的解释将是一个很好的参考资料!太棒了,非常感谢,@abarnert;我总是对Python2和Python3中的哪个是哪个感到困惑,所以您的解释将是一个很好的参考资料!