替换或转义& 在我的Python代码中不起作用。为什么?

用户名

两者都在交互式控制台(空闲)中工作。我的来源:

        from HTMLParser import HTMLParser
        parser = HTMLParser()
        a=autohist # autohist is a string containing source from selenium
        tags = []
        start = 0
        while a.find("alt=\\\"", start) != -1:
            find = a.find("alt=\\\"", start)+6
            print find
            titleartist = ""
            while a[find] != "\\":
                s = a[find]
                titleartist = titleartist + s
                find +=1
            start=find+1
            if titleartist=="Explicit Lyrics":
                continue
            titleartist = titleartist.replace("&","shit")
            #titleartist = parser.unescape(titleartist)
            print titleartist
            tags.append((titleartist.split(" - ")[1],titleartist.split(" - ")[0]))

但是当我使用Firefox硒示例获取html源时:

 titleartist="Diana Ross & The Supremes"

它印有这样的文字:Diana Ross shitamp; 至尊者

显然,我想替换&&,但是此示例显示了该问题。

亚当

不知何故,第一while部分对其进行了双重编码,因此解决方案是对其进行两次解码。例如:

titleartist.replace("&","&").replace("&","&")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Javascript所有点都用空格替换(为什么我的代码不起作用)

为什么我的字符串替换在JavaScript中不起作用?

我不知道为什么这个用于替换所有出现的子字符串的 python 代码不起作用

为什么进程替换在Shell脚本中不起作用?

为什么在 MATLAB 中替换符号矩阵不起作用?

为什么替换在Powershell中不起作用?

原来的代码运行良好,但是当我改用JSX替换React.createElement时,似乎不起作用,为什么?

为什么data-amp-bind-href或[href]在amp-bind组件中不起作用?

为什么我尝试在JSON中转义引号不起作用?

为什么换行转义字符在我的情况下不起作用?

为什么我的 C# 转义字符在从 base 64 解码时不起作用?

当我在x86 Assembly中打印它们时,ANSI转义代码不起作用?

从cin阅读时,为什么转义字符不起作用?

为什么转义符\ b在前几行不起作用?

为什么我的perl命令行替换不起作用?

为什么我的备用字符串替换方法不起作用?

为什么替换不起作用(不替换值)

在oracle中替换字符(&amp)

为什么我无法在sed替换中转义单引号但使用\ x27起作用?

替换字符串中的字母-为什么函数不起作用?

Makefile:为什么命令替换在$(shell)函数中不起作用?

尝试用不同.scn文件中的node替换ChildNode,为什么它不起作用?

为什么函数变量在函数内部的命令替换中不起作用?

为什么我的 Python 3 代码不起作用?

为什么文本修饰在我的代码中不起作用?

为什么符号“&”在我的scss代码中不起作用?

为什么 selectedIndex 在我的代码中不起作用?

为什么我的代码在Safari或Opera中不起作用?

为什么.append()在我的代码中不起作用?