Web Scraping Course for SEO – Class 7

Get metatag title

On each page we will obtain the title included in the html <title> tag

 

# -*- coding: iso-8859-15 -*-

# Web Scraping Course for SEO with Python
# Class 7: Get metatag title# webscrap7
import urllib
import re

url_sitio = “https://evginformatica.blogspot.com/”
url_inicio=url_sitio
lista_enlaces = []
titulos = []

def buscar_enlaces(posicion,busqueda):
   
    url = lista_enlaces[posicion]
   
    htmluni = urllib.urlopen(url).read()
    html=urllib.unquote(htmluni).decode(“utf-8”)
    #buscar <title>
    if titulos[posicion]==“”:
        titulo = re.findall(“<title>.+</title>”, html)
        if titulo<>[]:
            titulo_esp=titulo[0]
            titulos[posicion]=titulo_esp[7:-8]
   
   
    #buscar enlaces href
    enlaces = re.findall(busqueda, html)
    for enlace in enlaces:
   
        #quitar href=’ y quitar comilla final
        enlace2 = enlace[6:-1]
   
        #enlaces excluidos
        #/search
        # o que contienen #
        # solo paginas .html
        anadir=True
        if enlace2.find(“.html”)<0:
            anadir=False
       
        if enlace2.find(“/search”)>0:
            anadir=False
       
        if enlace2.find(“#”)>0:
            anadir=False
       
        if enlace2 in lista_enlaces:
            anadir=False
           
        if anadir:
            lista_enlaces.append(enlace2)
            titulos.append(“”)

def web_scrapin():
   
    print “— Enlaces internos —“
   
    lista_enlaces.append(url_inicio)
    titulos.append(“”)
    posicion = 0
   
    while True:
        #print “— “+lista_enlaces[posicion]
       
        busqueda = “href='”+url_sitio+“.+?'”
        buscar_enlaces(posicion,busqueda)
        busqueda = ‘href=”‘+url_sitio+‘.+?”‘
        buscar_enlaces(posicion,busqueda)
       
           
        posicion = posicion+1
        #pruebas
        #if posicion >= 2:
        #    break
       
        if posicion >= len(lista_enlaces):
            break
           
    i=1
    for lenlace in lista_enlaces:
        print str(i)+” “+lenlace       
        print ”     Titulo: “+titulos[i-1]
        i=i+1
   



if __name__ == “__main__”:  
    web_scrapin()
      

 

On each page that we find we look for the <title> tag with the regular expression “<title>. + </title>” and we are storing them in the titles list.

In the list that is shown on the screen we already see the titles of all the web pages of our website.

At this time the website administrator will be able to review the titles and if there are any wrong or nonexistent, modify the pages.

 

Leave a Reply

Your email address will not be published. Required fields are marked *