-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlog_topic_collator.py
More file actions
35 lines (28 loc) · 1.25 KB
/
Blog_topic_collator.py
File metadata and controls
35 lines (28 loc) · 1.25 KB
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
#! python3
# Collect writeups from blog sites and save them as .txt files with the topic of the blog as the name of the file
# and the content of the topic as the content of the .txt file.
import requests, bs4, os
print('Creating \'Copy_Blogger\' folder in your main directory...')
os.makedirs('Copy_Blogger', exist_ok=True)
blogSite = requests.get('https://copyblogger.com/')
blogSite.raise_for_status()
blogSoup = bs4.BeautifulSoup(blogSite.text, 'html.parser')
blogElem = blogSoup.select('.entry-title-link')
for i in range(len(blogElem)):
try:
heading = blogElem[i].get_text()
print('Creating ' + heading + '.txt' + '... in \'Copy_Blogger\' folder...')
file = open(os.path.join('Copy_Blogger', heading + '.txt'), 'w')
link = blogElem[i].get('href')
print('Getting the article link...')
blogContent = requests.get(link)
blogContent.raise_for_status()
contentSoup = bs4.BeautifulSoup(blogContent.text, 'html.parser')
contentElem = contentSoup.select('.entry-content')
content = contentElem[0].getText()
file.write(content)
file.close()
except:
print('Could\'nt create ' + heading + '.txt!')
continue
print('Done')