如何下载和读取带有通用换行符的 URL?
我将 urllib.urlopen 与 Python 2.7 一起使用,但我需要处理下载的 HTML 文档及其包含的换行符(在
元素内)。
urllib 文档 表明 urlopen 不会使用通用换行符。我该怎么做?
I was using urllib.urlopen
with Python 2.7, but I need to process the downloaded HTML document and its contained newlines (within a <pre>
element).
The urllib docs indicates urlopen will not use universal newlines. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非 HTML 文件已经在您的磁盘上,否则
urlopen()
将正确处理所有格式的换行符(\n
、\r\n
和\r
) 在您想要解析的 HTML 文件中(即将它们转换为\n
),根据 urllib 文档:例如
Unless the HTML file is already on your disk,
urlopen()
will handle correctly all formats of newlines (\n
,\r\n
and\r
) in the HTML file you want to parse (that is it will convert them to\n
), according to the urllib docs:E.g.
当您处理
pre
标记的内容时,请使用 splitlines 规范行结束符:When you process the contents of the
pre
tags, use splitlines to normalize the line-endings: