Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- from bs4 import BeautifulSoup
- path = "/home/edward/workspace/land_test/dating1"
- list_dirs = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
- list_html = [f for f in os.listdir(path) if f.endswith(".html")]
- extra_data = '''
- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/9.2.3/js/intlTelInput.min.js"></script>
- {% include '/global_static/html/helper.html' %}
- <script>var mobico = { project: {{ g.extproject.alt_id }}, container: "#mobico-terms" };</script>
- <script src="https://cdn.mobico.pro/js/terms.min.js"></script>
- '''
- def attr_replacer(path_to_html_file):
- """
- :param path_to_html_file: str - path to file
- :return: modified html file
- """
- html_file = open(path_to_html_file)
- soup = BeautifulSoup(html_file, "html5lib")
- for el in list_dirs:
- src_result = soup.find_all(src=re.compile('^'+el))
- href_result = soup.find_all(href=re.compile('^'+el))
- xhref_result = soup.find_all('use', attrs={'xlink:href': re.compile('^'+el)})
- for s in src_result:
- s['src'] = '/' + os.path.basename(path) + '/' + s['src']
- for h in href_result:
- h['href'] = '/' + os.path.basename(path) + '/' + h['href']
- for xh in xhref_result:
- xh['xlink:href'] = '/' + os.path.basename(path) + '/' + xh['xlink:href']
- soup.body.append(BeautifulSoup(extra_data, 'html.parser'))
- data = soup.prettify("utf-8")
- html_file = open(path_to_html_file, 'wb')
- html_file.write(str(data))
- html_file.close()
- for f in list_html:
- attr_replacer(os.path.join(path, f))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement