更换或拆卸&;在我的Python代码中不起作用。为什么?

更换或拆卸&;在我的Python代码中不起作用。为什么?,python,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

两者都在交互式控制台中工作。 我的消息来源:

        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]))
但是当我使用selenium Firefox获得html源代码时 例如:

它打印这个: 戴安娜·罗斯·希坦普;最高法院

显然,我想取代&;但这个例子说明了问题所在。

首先,whilepart对其进行了双重编码,因此解决方案是对其进行两次解码。例如:

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

我怀疑API获取的文本值是双重编码的?可能是a&;块预先存在而不是&?但是我如何解决它呢?就像替换解码&;但是为什么?你能给我们一个SSCCE吗?不清楚你在说什么,还有相当多的,呃,无关的东西。双关语:-看起来我简化得太多了,因为这是可行的:s=我们很快就拥有了它&;愤怒的s=s.替换&;,&印刷品
titleartist.replace("&","&").replace("&","&")