python - 如何在读取的 HTML 文档中翻译/转换 unicode 转义 < 和 >?

标签 python html beautifulsoup

当我使用 urllib2 opener 在 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e1b1d0b1c000f030b0e29232f2722602d2123" rel="noreferrer noopener nofollow">[email protected]</a>"
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

最佳答案

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">

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

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

关于python - 如何在读取的 HTML 文档中翻译/转换 unicode 转义 < 和 >?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6488706/

相关文章:

javascript - 如何使用同一按钮设置多个输入字段的值

html - 垂直居中到一个div

python - 如何创建 numpy 数组的不规则间隔切片的 View ?

python - 如何读取位图文件并在之后裁剪它?

python - docker healthcheck 显示在 django 中找不到

python - POST 请求无法与站点交互

python - Python2 和 Python3 之间的 BeautifulSoup HTMLparsingError

python追加错误索引1超出了尺寸为1的轴0的范围

javascript - 使用javascript访问设备相机

python - 提取 CSS 类中的文本