-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.py
80 lines (70 loc) · 2.25 KB
/
scrape.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Responsibility:
* Use webdriver to fetch a episode webpage
* DOM manipulation/selection
* Use `parse` to extract data that cannot be fetched using trivial regular expressions
"""
import re
from .parse import *
class Scrape(object):
def __init__(self, driver, url):
self.driver = driver
self.url = url
self.driver.get(self.url)
def _get_value(self, regex, selector):
lines = self.driver.find_element_by_css_selector(
selector).text.split("\n")
for line in lines:
match = re.match(
regex,
line,
re.MULTILINE | re.DOTALL
)
if match:
return match.group(1).strip()
def _get_transcript(self):
probably_transripts = self.driver.find_elements_by_css_selector('pre')
for element in probably_transripts:
for sibling in element.find_elements_by_xpath('preceding-sibling::h2'):
if re.search('transcript \{.*\}', sibling.text.lower()):
return element.text
def get_episode_info(self):
season = self._get_value(
'^\[(\d+)\.\d+\]',
'h1'
)
episode = self._get_value(
'^\[\d+\.(\d+)\]',
'h1'
)
title = self._get_value(
'\[.*\](.*)',
'#rightCol h1'
)
written_date = self._get_value(
'Transcript written on (.*)',
'#rightCol pre'
)
revised_date = self._get_value(
'Transcript revised on (.*)',
'#rightCol pre'
)
aired_date = self._get_value(
'Original Airdate on NBC: (.*)',
'#rightCol pre'
)
writers, director = get_director_and_writers(
self.driver.find_element_by_css_selector('#rightCol pre').text
)
return {
"season": season,
"episode": episode,
"title": title,
"written_date": written_date,
"revised_date": revised_date,
"aired_date": aired_date,
"writers": writers,
"director": director
}
def get_dialog_info(self):
return get_cast_dialog(self._get_transcript())