nicuf

parsing tags html (title, description, keywords) etc

Feb 2nd, 2022
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.90 KB | None | 0 0
  1. import requests
  2. import re
  3.  
  4. # Path to english folder 1
  5.  
  6. english_folder2 = r"c:\Folder1"
  7.  
  8. extension_file = ".html"
  9.  
  10. use_parse_folder = True
  11.  
  12. import os
  13.  
  14. en1_directory = os.fsencode(english_folder2)
  15. en2_directory = os.fsencode(english_folder2)
  16.  
  17. # These connection words will be ignore when parsing data from <title> tag to <meta keywords> tag
  18. LISTA_CUVINTE_LEGATURA = [
  19.     'in', 'la', 'unei', 'si', 'sa', 'se', 'de', 'prin', 'unde', 'care', 'a',
  20.     'al', 'prea', 'lui', 'din', 'ai', 'unui', 'acei', 'un', 'doar', 'tine',
  21.     'ale', 'sau', 'dintre', 'intre', 'cu','ce', 'va', 'fi', 'este', 'cand', 'o',
  22.     'cine', 'aceasta', 'ca', 'dar', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII',
  23.     'to', 'was', 'your', 'you', 'is', 'are', 'iar', 'fara', 'aceasta', 'pe', 'tu',
  24.     'nu', 'mai', 'ne', 'le', 'intr', 'cum', 'e', 'for', 'she', 'it', 'esti',
  25.     'this', 'that', 'how', 'can', 't', 'must', 'be', 'the', 'and', 'do', 'so', 'or', 'ori',
  26.     'who', 'what', 'if', 'of', 'on', 'i', 'we', 'they', 'them', 'but', 'where', 'by', 'an',
  27.     'on', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'made', 'make', 'my', 'me', '-',
  28.     'vom', 'voi', 'ei', 'cat', 'ar', 'putea', 'poti', 'sunteti', 'inca', 'still', 'noi', 'l',
  29.     'ma', 's', 'dupa', 'after', 'under', 'sub', 'niste', 'some', 'those', 'he'
  30. ]
  31.  
  32. def creeaza_lista_keywords(titlu):
  33.     # imparte titlul in 2 in functie de bara verticala |
  34.     prima_parte_titlu = titlu.split('|')[0]
  35.     # extrage toate cuvintele din prima parte a titlului
  36.     keywords = re.findall(r'(?:\w|-*\!)+', prima_parte_titlu)
  37.     # extrage keyword-urile care nu se gasesc in lista de cuvinte de legatura
  38.     keywords_OK = list()
  39.     for keyword in keywords:
  40.         if keyword not in LISTA_CUVINTE_LEGATURA:
  41.             # adauga keyword-ul cu litere mici
  42.             keywords_OK.append(keyword.lower())
  43.     # returneaza un string in care toate keyword-urile sunt alaturate prin ', '
  44.     return ", ".join(keywords_OK)
  45.  
  46.  
  47. print('Going through english folder')
  48. amount = 1
  49. for file in os.listdir(en1_directory):
  50.     filename = os.fsdecode(file)
  51.     print(filename)
  52.     if filename == 'y_key_e479323ce281e459.html' or filename == 'directory.html':
  53.         continue
  54.     if filename.endswith(extension_file):
  55.         with open(os.path.join(english_folder2, filename), encoding='utf-8') as html:
  56.             html = html.read()
  57.  
  58.             try:
  59.                 with open(os.path.join(english_folder2, filename), encoding='utf-8') as en_html:
  60.                     en_html = en_html.read()
  61.  
  62.                     # title to meta
  63.                     try:
  64.                         title = re.search('<title.+/title>', html)[0]
  65.                         title_content = re.search('>(.+)<', title)[1]
  66.                     except:
  67.                         pass
  68.  
  69.                     try:
  70.                         meta_og_title = re.search('<meta property="og:title".*>', en_html)[0]
  71.                         new_meta_og_title = re.sub(r'content=".+"', f'content="{title_content}"', meta_og_title)
  72.                         en_html = en_html.replace(meta_og_title, new_meta_og_title)
  73.                     except:
  74.                         pass
  75.  
  76.                     try:
  77.                         meta_keywords = re.search('<meta name="keywords".*>', en_html)[0]
  78.                         keywords = creeaza_lista_keywords(title_content)
  79.                         new_meta_keywords = re.sub(r'content=".+"', f'content="{keywords}"', meta_keywords)
  80.                         en_html = en_html.replace(meta_keywords, new_meta_keywords)
  81.                     except:
  82.                         pass
  83.  
  84.                     try:
  85.                         meta_abstract = re.search('<meta name="abstract".*>', en_html)[0]
  86.                         new_meta_abstract = re.sub(r'content=".+"', f'content="{title_content}"', meta_abstract)
  87.                         en_html = en_html.replace(meta_abstract, new_meta_abstract)
  88.                     except:
  89.                         pass
  90.  
  91.                     try:
  92.                         meta_Subject = re.search('<meta name="Subject".*>', en_html)[0]
  93.                         new_meta_Subject = re.sub(r'content=".+"', f'content="{title_content}"', meta_Subject)
  94.                         en_html = en_html.replace(meta_Subject, new_meta_Subject)
  95.                     except:
  96.                         pass
  97.  
  98.                     try:
  99.                         headline = re.search('"headline":.+', en_html)[0]
  100.                         new_headline = re.sub(r':.+', f': "{title_content}",', headline)
  101.                         en_html = en_html.replace(headline, new_headline)
  102.                     except:
  103.                         pass
  104.  
  105.                     try:
  106.                         keywords = re.search('"keywords":.+', en_html)[0]
  107.                         new_keywords = re.sub(r':.+', f': "{title_content}",', keywords)
  108.                         en_html = en_html.replace(keywords, new_keywords)
  109.                     except:
  110.                         pass
  111.  
  112.                     # canonical to meta og:url and @id
  113.                     try:
  114.                         canonical_content = re.search('<link rel="canonical" href="(.+)".*>', html)[1]
  115.                     except:
  116.                         pass
  117.  
  118.                     try:
  119.                         og_url = re.search('<meta property="og:url".*>', en_html)[0]
  120.                         new_og_url = re.sub(r'content=".+"', f'content="{canonical_content}"', og_url)
  121.                         en_html = en_html.replace(og_url, new_og_url)
  122.                     except:
  123.                         pass
  124.  
  125.                     try:
  126.                         id = re.search('"@id":.+', en_html)[0]
  127.                         new_id = re.sub(r':.+', f': "{canonical_content}"', id)
  128.                         en_html = en_html.replace(id, new_id)
  129.                     except:
  130.                         pass
  131.  
  132.                     # meta description to og:description and description
  133.                     try:
  134.                         meta = re.search('<meta name="description".+>', html)[0]
  135.                         meta_description = re.search('<meta name="description" content="(.+)".*>', html)[1]
  136.                     except:
  137.                         pass
  138.  
  139.                     try:
  140.                         og_description = re.search('<meta property="og:description".+/>', en_html)[0]
  141.                         new_og_description = re.sub(r'content=".+"', f'content="{meta_description}"', og_description)
  142.                         en_html = en_html.replace(og_description, new_og_description)
  143.                     except:
  144.                         pass
  145.  
  146.                     try:
  147.                         description = re.search('"description":.+', en_html)[0]
  148.                         new_description = re.sub(r':.+', f': "{meta_description}",', description)
  149.                         en_html = en_html.replace(description, new_description)
  150.                     except:
  151.                         pass
  152.  
  153.                     try:
  154.                         en_html = re.sub('<meta name="description".+/>', meta, en_html)
  155.                     except:
  156.                         pass
  157.  
  158.                     try:
  159.                         en_html = re.sub('<title.+/title>', title, en_html)
  160.                     except:
  161.                         pass
  162.             except FileNotFoundError:
  163.                 continue
  164.  
  165.         print(f'{filename} parsed ({amount})')
  166.         amount += 1
  167.         if use_parse_folder:
  168.             try:
  169.                 with open(os.path.join(english_folder2+r'', ''+filename), 'w', encoding='utf-8') as new_html:
  170.                     new_html.write(en_html)
  171.             except:
  172.                 os.mkdir(english_folder2+r'')
  173.                 with open(os.path.join(english_folder2+r'', ''+filename), 'w', encoding='utf-8') as new_html:
  174.                     new_html.write(en_html)
  175.         else:
  176.             with open(os.path.join(english_folder2, 'parsed_'+filename), 'w', encoding='utf-8') as html:
  177.                 html.write(en_html)
Add Comment
Please, Sign In to add comment