【问题标题】:How to translate/convert unicode escaped < and > in a read HTML doc?如何在读取的 HTML 文档中翻译/转换 unicode 转义的 < 和 >?
【发布时间】:2011-06-28 05:23:05
【问题描述】:

当我使用 urllib2 打开器在 python 中读取一些(但不是全部)HTML 文件时,在一些文件上,我得到的文本中包含大量反斜杠和 unicode 003c 字符串。我将此文本发送到 BeautifulSoup 并且无法使用 findAll() 找到我要查找的内容,我现在认为这是由于所有这些 un​​icode 字符串造成的。

这是怎么回事,我该如何摆脱它?

soup.prettify() 之类的方法无效。

这是一些示例代码(来自 Facebook 个人资料)

\\u003cdiv class=\\"pas status fcg\\">Loading...\\u003c\\/div>
\\u003c\\/div>\\u003cdiv class=\\"uiTypeaheadView fbChatBuddyListTypeaheadView dark hidden_elem\\" id=\\"u971289_14\\">\\u003c\\/div>
\\u003c\\/div>\\u003c\\/div>\\u003cdiv class=\\"fbNubFlyoutFooter\\">
\\u003cdiv class=\\"uiTypeahead uiClearableTypeahead fbChatTypeahead\\" id=\\"u971289_15\\">
\\u003cdiv class=\\"wrap\\">\\u003clabel class=\\"clear uiCloseButton\\" for=\\"u971291_21\\">

同样的 HTML 页面在“查看源代码”窗口中看起来很正常。

编辑:这是生成该文本的代码。奇怪的是,我没有从其他 HTML 页面得到这种输出。请注意,我在这里用 USERNAME 和 PASSWORD 替换了用户名和密码。如果你替换这两个,你可以在你自己的 FB 个人资料上试试这个。

fbusername = "USERNAME@gmail.com"
fbpassword = "PASSWORD"
cookiefile = "facebook.cookies"

cj = cookielib.MozillaCookieJar(cookiefile)
if os.access(cookiefile, os.F_OK):
    cf.load()

opener = urllib2.build_opener(
    urllib2.HTTPRedirectHandler(),
    urllib2.HTTPHandler(debuglevel=0),
    urllib2.HTTPSHandler(debuglevel=0),
    urllib2.HTTPCookieProcessor(cj)
)

opener.addheaders = [('User-agent','Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1'),('Referer','http://www.facebook.com/')]

def facebooklogin():
    logindata = urllib.urlencode({
        'email' : fbusername,
        'pass' : fbpassword,
    })

    response = opener.open("https://login.facebook.com/login.php",logindata)
    return ''.join(response.readlines())


print "Logging in to Facebook...\n"
facebooklogin()
facebooklogin()
print "Successful.\n"

fetchURL = 'http://www.facebook.com/USERNAME?ref=profile&v=info'

f = opener.open(fetchURL)
fba = f.read()
f.close()
soup = BeautifulSoup(fba)
print soup

【问题讨论】:

  • 发布生成此文本的代码。
  • 试试:unicodeTextFromUrlLib.encode("ascii","ignore") 我想说的是:&gt;&gt;&gt; s=u"\u003c" &gt;&gt;&gt; s u'&lt;' &gt;&gt;&gt; s.encode("ascii","ignore") '&lt;'

标签: python html beautifulsoup


【解决方案1】:

u""" 构造用于 Python 2。对于 Python 3,您省略了 u

>>> a=u"""\\u003cdiv class=\\"pas status fcg\\">Loading...\\u003c\\/div>
... \\u003c\\/div>\\u003cdiv class=\\"uiTypeaheadView fbChatBuddyListTypeaheadView dark hidden_elem\\" id=\\"u971289_14\\">\\u003c\\/div>
... \\u003c\\/div>\\u003c\\/div>\\u003cdiv class=\\"fbNubFlyoutFooter\\">
... \\u003cdiv class=\\"uiTypeahead uiClearableTypeahead fbChatTypeahead\\" id=\\"u971289_15\\">
... \\u003cdiv class=\\"wrap\\">\\u003clabel class=\\"clear uiCloseButton\\" for=\\"u971291_21\\">
... """
>>> print(a.decode('unicode_escape')).replace('\\/', '/')
<div class="pas status fcg">Loading...<\/div>
<\/div><div class="uiTypeaheadView fbChatBuddyListTypeaheadView dark hidden_elem" id="u971289_14"><\/div>
<\/div><\/div><div class="fbNubFlyoutFooter">
<div class="uiTypeahead uiClearableTypeahead fbChatTypeahead" id="u971289_15">
<div class="wrap"><label class="clear uiCloseButton" for="u971291_21">

我希望这会有所帮助。如果没有,请改进您在问题中提供的信息。

编辑:建议的答案现在也将 \/ 更改为 /

【讨论】:

  • 这仍然会留下带有&lt;\/div&gt;的结束标签。
  • 谢谢,这行得通。不过,仍然不确定为什么它首先会以这种方式出现(请参阅我对原始问题的编辑)。无论如何,解决这个问题似乎并没有解决我的 BeautifulSoup 问题。如果我无法弄清楚,我会发布一个新问题。
猜你喜欢
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 2015-02-11
相关资源
最近更新 更多