【问题标题】:Python urllib2.urlopen(url).read() is different from source code seen in FirefoxPython urllib2.urlopen(url).read() 与 Firefox 中看到的源代码不同
【发布时间】:2018-02-14 08:31:16
【问题描述】:

当我使用 urllib2.urlopen(url).read() 时,我读到的源代码与我在 Firefox 中读到的略有不同。 在 Firefox 中看到的源代码中,一些特殊字符,例如引号 (")、撇号 (') 等被转换为 %22、%27 等。

当我使用 urllib2.urlopen(url).read() 时,特殊字符可以明文读取。我想用 Python 看到网页的源代码,就像我用 Firefox 看到的一样(使用 %22、%27 等)。

谢谢你,对不起我的英语。

【问题讨论】:

  • 也许结帐selenium,和这个类似的问答:How to get real source code of html page?
  • 对不起,我不想使用 selenium,我刚读过。另一种方式?我用 Python 阅读了相同的完整源代码,但有些字符有不同的编码( ' = %27 )。为什么?

标签: python firefox urllib2 urlopen


【解决方案1】:

也许是urlencoded

您可以尝试转义结果。

data = urllib2.urlopen(url).read()
print(urllib.quote(data))

【讨论】:

  • 谢谢,但这不是我想要的结果。在网页的源代码中,字符 ' 用于标签的属性中,用于打开和关闭一个值,但是,例如,在属性 href 中,字符显示为 %27。使用 Firefox,我可以看到这种差异,而不是使用 urllib2。 Urllib2 以相同的方式读取 ' 和 %27。我不想使用硒。谢谢你的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2023-03-18
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
相关资源
最近更新 更多