Guest User

Find youtube link

a guest
Feb 19th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3. import re
  4. import os
  5.  
  6. defaultUrl = 'https://2ch.hk/v/res/7821935.html'
  7. defaultSavePath = 'C:\\'
  8.  
  9.  
  10. url = input('Введите URL: ')
  11. if len(url) < 4:
  12.     url = defaultUrl
  13.  
  14. text = BeautifulSoup(requests.get(url, allow_redirects=True, stream=True).text, "lxml")
  15.  
  16. HTML = '<style>html{background-color:#252525; color:#707070; font-size: 24px;}</style>\n\n'
  17. HTML += '<table width = "100%" border="2">\n'
  18.  
  19. for article in text.findAll("article"):
  20.  
  21.     if 'youtu' not in article.text:
  22.         continue
  23.  
  24.     HTML += '<tr>\n'
  25.     string = ''
  26.     for post in article.contents:
  27.  
  28.         bs = BeautifulSoup(str(post), "lxml").text
  29.  
  30.         bs = re.sub(r'>>\d+\s?\(?\w*\)?\s?', '', bs)
  31.         bs = re.sub(r'\s+\.\s+', '. ', bs)
  32.         bs = re.sub(r'\s+\,\s+', ', ', bs)
  33.  
  34.         if 'youtu' in bs:
  35.             bs = f'<a href="{bs.strip()}">{bs.strip()}</a><br />'
  36.  
  37.         string += ''.join([bs.lstrip() or '', ' '])
  38.  
  39.     string += '<br /><br />\n'
  40.     HTML += string
  41.     HTML += '</tr>\n'
  42. HTML += '</table>\n'
  43.  
  44. savepath = input('Укажите путь файла для сохранения: ')
  45.  
  46. if len(savepath) < 2:
  47.     savepath = defaultSavePath
  48.  
  49. with open(os.path.join(savepath, 'youtube link test.html'), "w", encoding="utf-8") as f:
  50.     f.write(HTML)
  51.  
  52. print(f'Сохранено в {os.path.join(savepath, "youtube link test.html")}')
Add Comment
Please, Sign In to add comment