两个外部包中的python冲突

rod*_*din 10 python conflict packages

我正在编写代码来组合python rawdog RSS阅读器库和BeautifulSoup webscraping库中的函数.在我想要克服的内脏中存在冲突.

我可以用这个简化的代码复制问题:

    import sys, gzip
    def scrape(filename):
        contents = gzip.open(filename,'rb').read()
        contents = contents.decode('utf-8','replace')
        import BeautifulSoup as BS
        print 'before rawdog: ', len(BS.BeautifulSoup(contents)) # prints 4, correct answer
        from rawdoglib import rawdog as rd
        print 'after rawdog: ', len(BS.BeautifulSoup(contents)) # prints 3, incorrect answer
Run Code Online (Sandbox Code Playgroud)

无论什么顺序或我在哪里进行导入,rawdog的导入总是导致BS.BeautifulSoup()方法返回错误的响应.当我需要BeautifulSoup时,我实际上不再需要rawdog,所以我在那时尝试删除了包,但是BS仍然坏了.我尝试过的修复程序没有用到:

  • 我注意到rawdog代码自己导入了BeautifulSoup.所以我尝试import BeautifulSoup从rawdog代码中删除并重新安装rawdog
  • 在导入BeautifulSoup之前删除rawdog模块:
    • for x in filter(lambda y: y.startswith('rawdog'), sys.modules.keys()): del sys.modules[x]
  • 从rawdog导入更具体的类/方法,例如 from rawdoglib.rawdog import FeedState
  • 在导入rawdog之前和之后给问题方法一个新名称: from BeautifulSoup import BeautifulSoup as BS
  • from __future__ import absolute_import

没有运气,我总是得到len(BeautifulSoup(内容))== 3如果将rawdog导入命名空间.这两个软件包都很复杂,以至于我无法确切地知道问题重叠是什么,而且我不知道用什么工具来解决这个问题,除了搜索dir(BeautifulSoup)和dir( rawdog),我没有找到好的线索.

更新,回答答案:我省略了每个输入文件都不会出现问题,这是至关重要的,对不起.有问题的文件非常大,所以我不认为我可以在这里发布它们.我将尝试找出好的和坏的文件之间的关键区别并发布它.感谢您到目前为止的调试帮助.

进一步调试!我已在输入文本中将此块标识为有问题:

    function SwitchMenu(obj){
      if(document.getElementById){
      var el = document.getElementById(obj);
      var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
         if(el.style.display != "block"){ //DynamicDrive.com change
         for (var i=0; i<ar.length; i++){
            if (ar[i].className=="submenu") //DynamicDrive.com change
            ar[i].style.display = "none";
      }
      el.style.display = "block";
      }else{
        el.style.display = "none";
    }
}
Run Code Online (Sandbox Code Playgroud)

}

如果我注释掉这个块,那么无论是否使用rawdog导入,我都可以通过BeautifulSoup获得正确的解析.使用块,rawdog + BeautifulSoup是错误的.那么我应该只搜索我的输入这样的块,还是有更好的解决方法?

lbo*_*lla 5

这是一个错误rawdoglib.feedparser.pyrawdog是猴子修补smglib:在第 198 行,它写道:

if sgmllib.endbracket.search(' <').start(0):
    class EndBracketMatch:
        endbracket = re.compile('''([^'"<>]|"[^"]*"(?=>|/|\s|\w+=)|'[^']*'(?=>|/|\s|\w+=))*(?=[<>])|.*?(?=[<>])''')
        def search(self,string,index=0):
            self.match = self.endbracket.match(string,index)
            if self.match: return self
        def start(self,n):
            return self.match.end(n)
    sgmllib.endbracket = EndBracketMatch()
Run Code Online (Sandbox Code Playgroud)

这是一个重现错误的脚本:

contents = '''<a><ar "none";                                                 
</a> '''                                                                     
import BeautifulSoup as BS                                                   
print 'before rawdog: ', len(BS.BeautifulSoup(contents)) # prints 4, correct answer
from rawdoglib import rawdog as rd                                           
print 'after rawdog: ', len(BS.BeautifulSoup(contents)) # prints 3, incorrect
Run Code Online (Sandbox Code Playgroud)

它在“a”标签内的“<”处中断。在 OP 的代码段中,它由以下行触发:(for (var i=0; i<ar.length; i++){注意“<”字符)。

在 rawdog 的 ML 上提交的问题:http ://lists.us-lot.org/pipermail/rawdog-users/2012-August/000327.html