Pregunta

I am having problems with this code as it is doing everything OK apart from writing the outputs in the "f file". Could anyone help me with this?

The problem is with this line: f.write(blog +' '+ authority +'\n')

        try:
            response = br.open(buyer)
            tree = html.fromstring(response.read())
            blogs = tree.xpath('//div/ul/li[@class="sidebar-item"]/a/@href')
            for blog in blogs:
                if '.blogspot.com' in blog:
                    try:
                        response = br.open(blog)
                    except HTTPError, e:

                        if 'http://www.blogger.com/create-blog.g?defaultSubdomain=' in e.read():
                            try:
                                response = br.open( 'http://www.opensiteexplorer.org/links?site=' + blog )
                            except Exception:
                                response = br.open( 'http://www.opensiteexplorer.org/links?site=' + blog )
                            tree = html.fromstring(response.read())
                            authority = int (tree.xpath('//span[@class="metrics-authority"]/text()')[1].strip())
                            if authority>1:
                                print blog
                                print 'This blog is ready to be registered'
                                print authority
                                f.write(blog +' '+ authority +'\n')
                        else:
                            print ''
        except Exception:
            print ''
print 'Finished'
f.close()
¿Fue útil?

Solución

Here's the problem:

When you're trying to do

f.write(blog +' '+ authority +'\n')

Python is throwing a ValueError because you can't add an int and a str. You're also catching every exception which is ill-advised. Change the offending line to convert authority to a str:

f.write(blog +' '+ authority +'\n')

And you should be okay. Get rid of the blanket except clause and only catch specific exceptions that you want to catch. While you're developing this code, I would recommend you don't catch any any exceptions just so you can tell why things are going wrong.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top