Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- import requests
- import re
- import os
- defaultUrl = 'https://2ch.hk/v/res/7821935.html'
- defaultSavePath = 'C:\\'
- url = input('Введите URL: ')
- if len(url) < 4:
- url = defaultUrl
- text = BeautifulSoup(requests.get(url, allow_redirects=True, stream=True).text, "lxml")
- HTML = '<style>html{background-color:#252525; color:#707070; font-size: 24px;}</style>\n\n'
- HTML += '<table width = "100%" border="2">\n'
- for article in text.findAll("article"):
- if 'youtu' not in article.text:
- continue
- HTML += '<tr>\n'
- string = ''
- for post in article.contents:
- bs = BeautifulSoup(str(post), "lxml").text
- bs = re.sub(r'>>\d+\s?\(?\w*\)?\s?', '', bs)
- bs = re.sub(r'\s+\.\s+', '. ', bs)
- bs = re.sub(r'\s+\,\s+', ', ', bs)
- if 'youtu' in bs:
- bs = f'<a href="{bs.strip()}">{bs.strip()}</a><br />'
- string += ''.join([bs.lstrip() or '', ' '])
- string += '<br /><br />\n'
- HTML += string
- HTML += '</tr>\n'
- HTML += '</table>\n'
- savepath = input('Укажите путь файла для сохранения: ')
- if len(savepath) < 2:
- savepath = defaultSavePath
- with open(os.path.join(savepath, 'youtube link test.html'), "w", encoding="utf-8") as f:
- f.write(HTML)
- print(f'Сохранено в {os.path.join(savepath, "youtube link test.html")}')
Add Comment
Please, Sign In to add comment