-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
168 lines (135 loc) · 4.87 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from bs4 import BeautifulSoup
from urllib.request import urlopen, urlretrieve
import json
from PIL import Image
from gethashtag import getHashtagFromHeaderNBA
def scrapeFox():
"""
Scrapes Fox News site for most recent headline
"""
quote_page = 'https://www.foxnews.com/'
page = urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
# Find Header text
header = soup.find('h3', attrs={'class': 'title'}).find('a')
headerText = header.text.strip()
# Find link to article
href = header.get('href')
# Convert headerText string to json
data = {
"text": f"{headerText}\n{href}",
}
jsonString = json.dumps(data)
jsonObj = json.loads(jsonString)
return jsonObj
def scrapeBingNews():
"""
Scrapes Bing news site for most recent headline
"""
quote_page = 'https://www.bing.com/news'
page = urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
# Find Header Text
article = soup.find('div', attrs={'class': 'news-card'})
header = article.get('data-title')
link = article.get('data-url')
# Convert headerText string to json
data = {
"text": f"{header}\n{link}",
}
jsonString = json.dumps(data)
jsonObj = json.loads(jsonString)
return jsonObj
def scrapeBingNBA():
"""
Scrapes Bing news for nba related articles
"""
quote_page = 'https://www.bing.com/news/search?q=NBA+News&qft=interval%3d%224%22&form=YFNR'
page = urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
# Find Header Text
article = soup.find('div', attrs={'class': 'news-card'})
header = article.get('data-title')
link = article.get('data-url')
# Download article image as source
image_tag = article.find('img', attrs={'class': 'rms_img'})
image_link = 'https://th.bing.com' + image_tag.get('src')
urlretrieve(image_link, "./images/source.jpg")
# Resize image to square (512x512)
image_to_upscale = Image.open('./images/source.jpg')
target_size = (512, 512)
original_width, original_height = image_to_upscale.size
width_ratio = target_size[0] / original_width
heigh_ratio = target_size[1] / original_height
resize_ratio = min(width_ratio, heigh_ratio)
new_width = int(original_width * resize_ratio)
new_height = int(original_height * resize_ratio)
resized_image = image_to_upscale.resize((new_width, new_height))
background = Image.new("RGB", target_size)
paste_x = (target_size[0] - new_width) // 2
paste_y = (target_size[1] - new_height) // 2
background.paste(resized_image, (paste_x, paste_y))
background.save('./images/source.jpg')
image_to_upscale.close()
# Convert headerText string to json
data = {
"text": f"{header}\n{link}",
}
jsonString = json.dumps(data)
jsonObj = json.loads(jsonString)
return jsonObj
def scrapeYahooNBA():
"""
Scrapes yahoo news for nba related articles
"""
quote_page = 'https://sports.yahoo.com/nba/'
page = urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
# Find Header Text
article = soup.find('div', attrs={'class': 'Cf'}).find('a')
header = article.text.strip()
link = article.get('href')
# print(header)
# print(link)
# Download article image as source
image_tag = soup.find('img', attrs={'class': 'W(100%)'})
image_link = image_tag.get('src')
# print(image_link)
urlretrieve(image_link, "./images/source.jpg")
# Resize image to square (512x512)
image_to_upscale = Image.open('./images/source.jpg')
target_size = (512, 512)
original_width, original_height = image_to_upscale.size
width_ratio = target_size[0] / original_width
heigh_ratio = target_size[1] / original_height
resize_ratio = min(width_ratio, heigh_ratio)
new_width = int(original_width * resize_ratio)
new_height = int(original_height * resize_ratio)
resized_image = image_to_upscale.resize((new_width, new_height))
background = Image.new("RGB", target_size)
paste_x = (target_size[0] - new_width) // 2
paste_y = (target_size[1] - new_height) // 2
background.paste(resized_image, (paste_x, paste_y))
background.save('./images/source.jpg')
image_to_upscale.close()
# Generate Hashtag from headline if possible
hashtagArr = getHashtagFromHeaderNBA(header)
hashtag1 = "#NBA"
# Convert headerText string to json
data = {
"text": f"{header}\n{hashtag1}\n{link}",
}
if len(hashtagArr) == 1:
hashtag2 = hashtagArr[0]
data = {
"text": f"{header}\n{hashtag1} {hashtag2}\n{link}",
}
if len(hashtagArr) >= 2:
hashtag2 = hashtagArr[0]
hashtag3 = hashtagArr[1]
data = {
"text": f"{header}\n{hashtag1} {hashtag2} {hashtag3}\n{link}",
}
jsonString = json.dumps(data)
jsonObj = json.loads(jsonString)
return jsonObj