-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_horsepaste.py
49 lines (40 loc) · 1.89 KB
/
parse_horsepaste.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
from bs4 import BeautifulSoup
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
import warnings
warnings.filterwarnings("ignore", message="_CFURLCopyResourcePropertyValuesAndFlags failed "
"because it was passed an URL which has no scheme")
def parse(url):
def render(source_url):
"""Fully render HTML, JavaScript and all."""
class Render(QWebEngineView):
def __init__(self, url):
self.html = None
self.app = QApplication(sys.argv)
QWebEngineView.__init__(self)
self.loadFinished.connect(self._loadFinished)
# self.setHtml(html)
self.load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
# This is an async call, you need to wait for this
# to be called before closing the app
self.page().toHtml(self._callable)
def _callable(self, data):
self.html = data
# Data has been stored, it's safe to quit the app
self.app.quit()
return Render(source_url).html
all_html = render(url)
soup = BeautifulSoup(all_html, "html.parser")
blue_hidden_tags = soup.findAll('div', {"class": "cell blue hidden-word"})
blue = [tag.span.text for tag in blue_hidden_tags]
red_hidden_tags = soup.findAll('div', {"class": "cell red hidden-word"})
red = [tag.span.text for tag in red_hidden_tags]
neutral_hidden_tags = soup.findAll('div', {"class": "cell neutral hidden-word"})
neutral = [tag.span.text for tag in neutral_hidden_tags]
black_hidden_tags = soup.findAll('div', {"class": "cell black hidden-word"})
black = [tag.span.text for tag in black_hidden_tags]
return [blue, red, neutral, black]