-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscraper.py
66 lines (50 loc) · 1.77 KB
/
scraper.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from markdownify import markdownify as md
import os
driver = webdriver.Chrome()
driver.get("https://pythnetwork.medium.com/")
# scroll to bottom
SCROLL_PAUSE_TIME = 5.0
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
article_number = 1
post_links = []
while True:
try:
expl = driver.find_element(By.XPATH, f'//*[@id="root"]/div/div[3]/div[2]/div/main/div/div[2]/div/div/article[{article_number}]/div/div/div/div/div/div[2]/div/div[1]/div/div/div/div[1]/div[1]/div[1]/a')
post_links.append( expl.get_attribute("href") )
article_number += 1
except:
print(f"Got {article_number-1} article links!")
break
driver.close()
# write to markdown
for i in range(len(post_links)):
for try_number in range(3):
driver = webdriver.Chrome()
driver.get(post_links[i])
time.sleep(3.0)
expl = driver.find_element(By.CSS_SELECTOR, '.cb')
try:
content = md(expl.get_attribute("innerHTML"))
break
except:
driver.close()
if not os.path.exists("training/blog"):
os.makedirs("training/blog")
with open(f"training/blog/article_{i}.md", 'w+') as f:
f.write(content)
driver.close()