1

I 'm trying to solve this question for 3 hours. Please can you tell me what is wrong? These are my codes

import urllib
from jin import HtmlMigrate
from bs4 import BeautifulSoup
import logging
list2=[]
logging.basicConfig( format='%(levelname)s:%(message)s', level=logging.INFO)
current = 1
source =""
for v in range(13960581, 13960585):
    list=[]
    try:
        fil = urllib.urlopen(source+str(v))
        fill = fil.read()
        soup = BeautifulSoup(fill)
        k = soup.find("div", "post-taglist")
        for i in k.findAll("a"):
            list.append(i.string)
    except AttributeError:
        pass
    if "python" in list:
        try:
            a = soup.find( "div", "post-text")
            list2.append(a)
            logging.info("%s question localized." % str(current))

        except AttributeError:
            pass

    current +=1
mig = HtmlMigrate()

out = file("stackover.html", "w")
for i in list2:
    mig.run(i, out)


out.close()

The problem is that I get duplicated entries in the console for each logger.info call. How can I solve this?

8
  • Can you show us your console output? Commented Jul 24, 2013 at 17:48
  • have you printed list? are you sure you don't have any repetition of "python" in 'list'? Commented Jul 24, 2013 at 17:48
  • Waiiit... where is list2 coming from? How does this code even reach the logger when it should be NameErroring first? Commented Jul 24, 2013 at 17:51
  • @user2357112 he simply forgot that piece in the code :D Commented Jul 24, 2013 at 17:53
  • Please make sure the code you post accurately reflects your problem. That means never retype your code into the question box. Copy and paste it. Commented Jul 24, 2013 at 17:56

3 Answers 3

1

Here's my stab: you're not seeing duplicate log entries. Rather, because your logging call is in a loop, you're seeing distinct calls to logging.info.

One way to (dis)confirm this is to change your logging call to include v:

logging.info("[%s] %s question localized." % (v, str(current)))

Let us know what that output looks like.


EDIT: OP has disconfirmed my theory; this answer is defunct.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the output : INFO:[13960581] 1 question localized. INFO:[13960581] 1 question localized. Whatever I do, It duplicated :(( Where am I wrong ?
1

Set a breakpoint just before the logging.info() call using

import pdb; pdb.set_trace()

When you hit the breakpoint and are at the pdb prompt, type

p logging.getLogger().handlers

You should see a list printed with just one element - a StreamHandler. If that is not the case, then that would explain why you're getting multiple lines for a single logging.info() call.

Comments

0

This material is from the standard documentation for the logging module discussing the use of logging.basicConfig:

Note: This function should be called from the main thread before other threads are started. In versions of Python prior to 2.7.1 and 3.2, if this function is called from multiple threads, it is possible (in rare circumstances) that a handler will be added to the root logger more than once, leading to unexpected results such as messages being duplicated in the log.

If this is being called from some thread other than your main program thread, it may be the problem. I know this should have been a comment but I do not have sufficient reputation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.