Advertisement
nicuf

Parsing-Batch Processor

Jun 24th, 2021 (edited)
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.74 KB | None | 0 0
  1.  
  2. English: https://neculaifantanaru.com/en/creating-a-batch-processing-python-with-regex-and-html-tags-parsing.html
  3. Romanian:  https://neculaifantanaru.com/creating-a-batch-processing-python-with-regex-and-html-tags-parsing.html
  4.  
  5.  
  6.  
  7. import requests
  8. import re
  9.  
  10. # Path to english folder 1
  11. english_folder1 = r"c:\Folder3"
  12.  
  13. # Path to english folder 2
  14. english_folder2 = r"c:\Folder3"
  15.  
  16. extension_file = ".html"
  17.  
  18. use_parse_folder = True #Face folder nou daca pui True, iar daca pui False redenumeste fisierele in acelasi folder
  19.  
  20. import os
  21.  
  22. en1_directory = os.fsencode(english_folder1)
  23. en2_directory = os.fsencode(english_folder2)
  24.  
  25. print('Going through english folder')
  26. for file in os.listdir(en1_directory):
  27.     filename = os.fsdecode(file)
  28.     print(filename)
  29.     if filename == 'y_key_e479323ce281e459.html' or filename == 'TS_4fg4_tr78.html':
  30.         continue
  31.     if filename.endswith(extension_file):
  32.         with open(os.path.join(english_folder1, filename), encoding='utf-8') as html:
  33.             html = html.read()
  34.  
  35.             try:
  36.                 with open(os.path.join(english_folder2, filename), encoding='utf-8') as en_html:
  37.                     en_html = en_html.read()
  38.  
  39.                     if False:  # if True: will Parse also the content that starts from <!-- ARTICOL START --> to <!-- ARTICOL FINAL --> and so on
  40.                         try:
  41.                             comment_body = re.search('<!-- ARTICOL START -->.+<!-- ARTICOL FINAL -->', html, flags=re.DOTALL)[0]
  42.                             en_html = re.sub('<!-- ARTICOL START -->.+<!-- ARTICOL FINAL -->', comment_body, en_html, flags=re.DOTALL)
  43.                         except:
  44.                             pass
  45.  
  46.                         try:
  47.                             comment_body2 = re.search('<!-- FLAGS_1 -->.+<!-- FLAGS -->', html, flags=re.DOTALL)[0]
  48.                             en_html = re.sub('<!-- FLAGS_1 -->.+<!-- FLAGS -->', comment_body2, en_html, flags=re.DOTALL)
  49.                         except:
  50.                             pass
  51.  
  52.                         try:
  53.                             comment_body3 = re.search('<!-- MENIU BARA SUS -->.+<!-- SFARSIT MENIU BARA SUS -->', html, flags=re.DOTALL)[0]
  54.                             en_html = re.sub('<!-- MENIU BARA SUS -->.+<!-- SFARSIT MENIU BARA SUS -->', comment_body3, en_html, flags=re.DOTALL)
  55.                         except:
  56.                             pass
  57.  
  58.                     # title to meta
  59.                     try:
  60.                         title = re.search('<title.+/title>', html)[0]
  61.                         title_content = re.search('>(.+)<', title)[1]
  62.                     except:
  63.                         pass
  64.  
  65.                     try:
  66.                         meta_og_title = re.search('<meta property="og:title".*>', en_html)[0]
  67.                         new_meta_og_title = re.sub(r'content=".+"', f'content="{title_content}"', meta_og_title)
  68.                         en_html = en_html.replace(meta_og_title, new_meta_og_title)
  69.                     except:
  70.                         pass
  71.  
  72.                     try:
  73.                         meta_keywords = re.search('<meta name="keywords".*>', en_html)[0]
  74.                         new_meta_keywords = re.sub(r'content=".+"', f'content="{title_content}"', meta_keywords)
  75.                         en_html = en_html.replace(meta_keywords, new_meta_keywords)
  76.                     except:
  77.                         pass
  78.  
  79.                     try:
  80.                         meta_abstract = re.search('<meta name="abstract".*>', en_html)[0]
  81.                         new_meta_abstract = re.sub(r'content=".+"', f'content="{title_content}"', meta_abstract)
  82.                         en_html = en_html.replace(meta_abstract, new_meta_abstract)
  83.                     except:
  84.                         pass
  85.  
  86.                     try:
  87.                         meta_Subject = re.search('<meta name="Subject".*>', en_html)[0]
  88.                         new_meta_Subject = re.sub(r'content=".+"', f'content="{title_content}"', meta_Subject)
  89.                         en_html = en_html.replace(meta_Subject, new_meta_Subject)
  90.                     except:
  91.                         pass
  92.  
  93.                     try:
  94.                         headline = re.search('"headline":.+', en_html)[0]
  95.                         new_headline = re.sub(r':.+', f': "{title_content}",', headline)
  96.                         en_html = en_html.replace(headline, new_headline)
  97.                     except:
  98.                         pass
  99.  
  100.                     try:
  101.                         keywords = re.search('"keywords":.+', en_html)[0]
  102.                         new_keywords = re.sub(r':.+', f': "{title_content}",', keywords)
  103.                         en_html = en_html.replace(keywords, new_keywords)
  104.                     except:
  105.                         pass
  106.  
  107.                     # canonical to meta og:url and @id
  108.                     try:
  109.                         canonical_content = re.search('<link rel="canonical" href="(.+)".*>', html)[1]
  110.                     except:
  111.                         pass
  112.  
  113.                     try:
  114.                         og_url = re.search('<meta property="og:url".*>', en_html)[0]
  115.                         new_og_url = re.sub(r'content=".+"', f'content="{canonical_content}"', og_url)
  116.                         en_html = en_html.replace(og_url, new_og_url)
  117.                     except:
  118.                         pass
  119.  
  120.                     try:
  121.                         id = re.search('"@id":.+', en_html)[0]
  122.                         new_id = re.sub(r':.+', f': "{canonical_content}"', id)
  123.                         en_html = en_html.replace(id, new_id)
  124.                     except:
  125.                         pass
  126.  
  127.                     # meta description to og:description and description
  128.                     try:
  129.                         meta = re.search('<meta name="description".+/>', html)[0]
  130.                         meta_description = re.search('<meta name="description" content="(.+)".+>', html)[1]
  131.                     except:
  132.                         pass
  133.  
  134.                     try:
  135.                         og_description = re.search('<meta property="og:description".+/>', en_html)[0]
  136.                         new_og_description = re.sub(r'content=".+"', f'content="{meta_description}"', og_description)
  137.                         en_html = en_html.replace(og_description, new_og_description)
  138.                     except:
  139.                         pass
  140.  
  141.                     try:
  142.                         description = re.search('"description":.+', en_html)[0]
  143.                         new_description = re.sub(r':.+', f': "{meta_description}",', description)
  144.                         en_html = en_html.replace(description, new_description)
  145.                     except:
  146.                         pass
  147.  
  148.                     try:
  149.                         en_html = re.sub('<meta name="description".+/>', meta, en_html)
  150.                     except:
  151.                         pass
  152.  
  153.                     try:
  154.                         en_html = re.sub('<title.+/title>', title, en_html)
  155.                     except:
  156.                         pass
  157.             except FileNotFoundError:
  158.                 continue
  159.  
  160.         print(f'{filename} parsed')
  161.         if use_parse_folder:
  162.             try:
  163.                 with open(os.path.join(english_folder2+r'\parsed', 'parsed_'+filename), 'w', encoding='utf-8') as new_html:
  164.                     new_html.write(en_html)
  165.             except:
  166.                 os.mkdir(english_folder2+r'\parsed')
  167.                 with open(os.path.join(english_folder2+r'\parsed', 'parsed_'+filename), 'w', encoding='utf-8') as new_html:
  168.                     new_html.write(en_html)
  169.         else:
  170.             with open(os.path.join(english_folder2, 'parsed_'+filename), 'w', encoding='utf-8') as html:
  171.                 html.write(en_html)
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement