-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedStateScraper.py
78 lines (50 loc) · 1.76 KB
/
RedStateScraper.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
################################################################################
# Will Maxcy
# Red State blog scraper
# Last Edited: 4/22/2021
################################################################################
from bs4 import BeautifulSoup as s
from urllib.request import Request, urlopen
import sys
import re
import csv
# scraper of individual blogs
def scrape(url):
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
soup = s(webpage, "lxml")
title = soup.find('title')
date = re.findall(r'[0-9][0-9][0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]',url)
author = soup.find('meta', attrs={'name':'author'})
body = soup.find_all('p')
body = body[:-1]
textTitle = title.get_text().replace(',','')[:-11]
textDate = date[0].replace('/','-')
textAuthor = author['content']
textBody = textTitle + ', '+ textAuthor + ", " + textDate + ", "
for text in body:
textBody = textBody + text.get_text().replace(',','')+' '
textBody = textBody + '\n'
blogTitle = 'C RedState '+textAuthor+' '+textTitle+'.csv'
invalid = '<>:\"\\|?*\'/'
for char in invalid:
blogTitle = blogTitle.replace(char,'')
print("RedState: "+textTitle)
file = open(sys.path[0]+"/SavedBlogs/"+blogTitle , "w+", encoding = 'utf-8')
file.write(textBody)
file.close()
return()
def main():
for x in range(1,10):
url = 'https://redstate.com/page/' + str(x) +'?ordinal='+ str((x-1)*9 + 1)+'&id=759800'
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
soup = s(webpage, "html.parser")
links = soup.find_all(class_='wp-card__img')
linkList = []
for link in links:
linkList.append(str('https://redstate.com') + link.find('a').get('href'))
for link in linkList:
scrape(link)
return()
main()