Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import os
  2. import re
  3. from bs4 import BeautifulSoup
  4. path = "/home/edward/workspace/land_test/dating1"
  5. list_dirs = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
  6. list_html = [f for f in os.listdir(path) if f.endswith(".html")]
  7.  
  8. extra_data = '''
  9. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/9.2.3/js/intlTelInput.min.js"></script>
  11. {% include '/global_static/html/helper.html' %}
  12. <script>var mobico = { project: {{ g.extproject.alt_id }}, container: "#mobico-terms" };</script>
  13. <script src="https://cdn.mobico.pro/js/terms.min.js"></script>
  14. '''
  15.  
  16.  
  17. def attr_replacer(path_to_html_file):
  18. """
  19. :param path_to_html_file: str - path to file
  20. :return: modified html file
  21. """
  22.  
  23. html_file = open(path_to_html_file)
  24. soup = BeautifulSoup(html_file, "html5lib")
  25.  
  26. for el in list_dirs:
  27. src_result = soup.find_all(src=re.compile('^'+el))
  28. href_result = soup.find_all(href=re.compile('^'+el))
  29. xhref_result = soup.find_all('use', attrs={'xlink:href': re.compile('^'+el)})
  30. for s in src_result:
  31. s['src'] = '/' + os.path.basename(path) + '/' + s['src']
  32. for h in href_result:
  33. h['href'] = '/' + os.path.basename(path) + '/' + h['href']
  34. for xh in xhref_result:
  35. xh['xlink:href'] = '/' + os.path.basename(path) + '/' + xh['xlink:href']
  36. soup.body.append(BeautifulSoup(extra_data, 'html.parser'))
  37. data = soup.prettify("utf-8")
  38. html_file = open(path_to_html_file, 'wb')
  39. html_file.write(str(data))
  40. html_file.close()
  41.  
  42. for f in list_html:
  43. attr_replacer(os.path.join(path, f))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement