Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting

Archive for September, 2010

Проверка количества проиндексированных страниц в Google

Tuesday, September 7th, 2010

Как определить кол-во проиндексированных страниц в гугле? реализация на Python. Недавно нужно было срочно, написал :

import urllib
import re

from urllib2 import urlopen
from urlparse import urlparse
from urllib import FancyURLopener

class GOpener(FancyURLopener):
    version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6'

def web_getpage(url):
    g_opener = GOpener()
    page = g_opener.open(url)
    return page.read()

def get_index(url):
    request = 'http://www.google.com/search?q=site:' + url
    # About 105,000,000 results
    index = 0
    try:
        gs = web_getpage(request)
        p = re.compile('About (.*) results')
        index = int(p.findall(gs)[0].replace(",", ""))
    except SearchError, e:
        index = -1
        #print "Search Failed : %s" % e
    finally:
        #print "Index: %d\tURL: %s" % (int(index), url)
        return index

Проверка подключения к интернету

Thursday, September 2nd, 2010
from urllib2 import urlopen
""" Returns True if we appear to have an internet connection or False.
    It defaults to using google as a test server, but you can supply an alternative if you want."""
def isonline(reliableserver='http://www.google.com'):
    try:
        urlopen(reliableserver)
        return True
    except IOError:
        return False

частенько нужно проверять есть ли коннект к интернету во время выполнения какого-либо скрипта. озадачился недавно, т.к. провайдер несколько напрягал пару дней.

My blog is Do-Follow


Пишу код, делаю сайты.
Check out my about.me profile!

парсинг сайтов, форумов, интернет магазинов

Want to subscribe?

istinspring twitter account
istinspring facebook account

 Subscribe in a reader Or, subscribe via email:
Enter your email address:  
Find entries :