-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
53 lines (39 loc) · 1.41 KB
/
main.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
from bs4 import BeautifulSoup
import html2text
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import sys
def main():
if len(sys.argv) == 2:
url = sys.argv[1]
else:
url = raw_input("Please enter the URL of the article: ")
# Load the URL in PhantomJS.
driver = webdriver.PhantomJS()
# driver = webdriver.Firefox()
driver.set_window_size(1280, 720)
driver.get(url)
WebDriverWait(driver, 45).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'p'))
)
soup = BeautifulSoup(driver.page_source)
driver.quit()
ps = soup.html.find_all("p")
biggest_parent = None
for p in ps:
# How many p tags does this parent have?
child_ps = len(p.parent.find_all('p', recursive=False))
if not biggest_parent or child_ps > biggest_parent[0]:
biggest_parent = (child_ps, p.parent)
# We most likely have the article container stored as biggest_parent[1], so
# turn it into Markdown.
print "---"
print u"title: \"{}\"".format(soup.title.string.strip()).encode('utf-8')
print u"canonical: {}".format(url).encode('utf-8').strip()
print "---"
print
print html2text.html2text(unicode(biggest_parent[1])).encode('utf-8')
if __name__ == '__main__':
main()