python - Mechanize 错误 - Python

标签 python mechanize

我正在尝试使用 Mechanize 来查看和收集来自各种网站的数据。我遇到了一个错误,指出

No handlers could be found for logger "mechanize"

此错误没有给出错误发生的具体位置,这使得调试非常困难。我在网上看了看,mechanize 0.1.11版本已经解决了这个问题。我已经下载了更新版本的 Mechanize ,并且想象这应该不再是一个问题。我导致这个问题的代码是
def KEGG(textfile):  
    KEGGString = ''  
    KEGGList = []  
    original = open('Final.txt', 'r')    
    for row in original:      
        data = re.split("(#|\t)", row) ##splits text files by tabs into separate strings  
        new_items = [x if x!='\t' else 'None' for x in data]  ##replaces extra tabs with "None"  
        KEGGList.append(new_items[-1])  
    KEGGHeaderless = KEGGList[1:] ## Removes header (Cross-Reference (KEGG)) from the list.  
    KEGGHeaderless = [s.replace(';', '') for s in KEGGHeaderless]  
    fo = open('KEGG Description.txt', 'w')  
    for row in KEGGHeaderless:  
       geneID = row  
       base2 = 'http://www.genome.jp/dbget-bin/www_bget?'  
       url = base2 + geneID  
       br = Browser()  
       br.set_handle_robots(False)  
       br.open(url)  
       for line in KEGGHeaderless:  
            targetURL = '/dbget-bin/get_linkdb?-t+pathway+' + row  
            try:  
                site = br.follow_link(url=targetURL)  
                br.open(site)  
                page = br.response().read()  
                fo.write(page)  
            except:   
                fo.write(line + "None\n")   
    fo.close()

有关如何解决此问题的任何建议?

最佳答案

mechanize使用 logging模块。
如果您的脚本不提供日志处理程序,则从 mechanize 中记录调用可能会导致此错误。

要提供通用处理程序,请尝试添加

import logging
logging.basicConfig(level=logging.DEBUG)

到你的脚本。这将显示级别 DEBUG 的所有日志消息或更高。
如果只想显示严重错误,可以将其更改为
logging.basicConfig(level=logging.CRITICAL)

或者,如果您想禁用日志记录,您可以使用
logging.disable(logging.DEBUG)

the docs对于其他选项和日志记录级别。

关于python - Mechanize 错误 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6472531/

相关文章:

python - 如何在 django 上为 Ext.data.TreeStore 的选定值正确创建响应

python - 为什么 pyenv 在 Alpine Linux 下找不到 openssl 共享库?

ruby - Mechanize 和 NTLM 身份验证

python - 爬虫的响应式网页设计检测

ruby - 如何在使用 Ruby Mechanize 加载页面之前设置 Referer header ?

ruby - Mechanize 发送 POST 以填充页面上的数据

python - elastic beanstalk 上的 wsgi 用户权限

python - 我如何在 python 中进行 strtotime?

python - 运行模块时如何使 __name__ == '__main__'

ruby-on-rails - 如何使用ruby在 "<a>"标签中找到href元素值