forked from aredoubles/data_dive_adl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArticlesOnHate.py
136 lines (106 loc) · 3.84 KB
/
ArticlesOnHate.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import requests
import bs4
import feedparser
import pandas as pd
import LocalCrawl
# NY Times Hate Crimes feed
def nyt_hate():
nytrss = feedparser.parse(
'https://www.nytimes.com/svc/collections/v1/publish/http://www.nytimes.com/topic/subject/hate-crimes/rss.xml')
nythate = {}
for i in range(10):
idlink = nytrss['entries'][i]['link']
res = requests.get(nytrss['entries'][i]['link'])
res.raise_for_status()
starch = bs4.BeautifulSoup(res.text, 'html5lib')
# The below is specific to the NYT
thetext = starch.find_all(class_='story-body-text')
full = ''
for tag in thetext:
full += (tag.text.strip() + ' ')
nythate[idlink] = full
build = pd.DataFrame.from_dict(nythate, orient='index')
build.columns = ['Text']
build['Source'] = 'New York Times'
build['Hate crime'] = 1
build.index.names = ['URL']
build.to_csv('NYT1.csv')
return build
# NY Times feed on NY local news
def nyt_local():
nytrss = feedparser.parse(
'https://www.nytimes.com/svc/collections/v1/publish/https://www.nytimes.com/section/nyregion/rss.xml')
nytnon = {}
for i in range(10):
idlink = nytrss['entries'][i]['link']
res = requests.get(nytrss['entries'][i]['link'])
res.raise_for_status()
starch = bs4.BeautifulSoup(res.text, 'html5lib')
# The below is specific to the NYT
thetext = starch.find_all(class_='story-body-text')
full = ''
for tag in thetext:
full += (tag.text.strip() + ' ')
nytnon[idlink] = full
build = pd.DataFrame.from_dict(nytnon, orient='index')
build.columns = ['Text']
build['Source'] = 'New York Times'
build['Hate crime'] = 0
build.index.names = ['URL']
build.to_csv('NYT0.csv')
return build
# Guardian feed on hate crime
def guardian_hate():
guardrss = feedparser.parse(
'https://www.theguardian.com/society/hate-crime/rss')
guardhate = {}
for i in range(20):
idlink = guardrss['entries'][i]['link']
res = requests.get(guardrss['entries'][i]['link'])
res.raise_for_status()
starch = bs4.BeautifulSoup(res.text, 'html5lib')
# The below is specific to The Guardian
thetext = starch.find_all('div', itemprop='articleBody')
for tag in thetext:
string = tag.text.strip()
string = string.replace('\r', ' ').replace('\n', ' ')
full = string
guardhate[idlink] = full
build = pd.DataFrame.from_dict(guardhate, orient='index')
build.columns = ['Text']
build['Source'] = 'The Guardian'
build['Hate crime'] = 1
build.index.names = ['URL']
build.to_csv('Guardian1.csv')
return build
def guardian_uk():
guardrss = feedparser.parse(
'https://www.theguardian.com/uk-news/rss')
guarduk = {}
for i in range(20):
idlink = guardrss['entries'][i]['link']
res = requests.get(guardrss['entries'][i]['link'])
res.raise_for_status()
starch = bs4.BeautifulSoup(res.text, 'html5lib')
# The below is specific to The Guardian
thetext = starch.find_all('div', itemprop='articleBody')
for tag in thetext:
string = tag.text.strip()
string = string.replace('\r', ' ').replace('\n', ' ')
full = string
guarduk[idlink] = full
build = pd.DataFrame.from_dict(guarduk, orient='index')
build.columns = ['Text']
build['Source'] = 'The Guardian'
build['Hate crime'] = 0
build.index.names = ['URL']
build.to_csv('Guardian0.csv')
return build
if __name__ == '__main__':
nyt1 = nyt_hate()
nyt0 = nyt_local()
grd1 = guardian_hate()
grd0 = guardian_uk()
local1 = LocalCrawl.localart()
articles = pd.concat([nyt1, nyt0, grd1, grd0, local1])
articles.to_csv('Articles.csv')