-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamp.py
28 lines (26 loc) · 1.16 KB
/
camp.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
from bs4 import BeautifulSoup as bs4
import requests
def getBlogData(value):
try:
#getting all the values
if (value == 'all'):
x = requests.get('https://www.freecodecamp.org/news/').text
else:
# getting the response based on particular tag
x = requests.get(f"https://www.freecodecamp.org/news/tag/{value}").text
soup=bs4(x,'lxml')
hack = soup.find_all('article',class_ = 'post-card')
#initializing a dictionary
val = {}
val["blog_data"] = []
for i in range(0,len(hack)):
data = {}
data["Tag"] = hack[i].find('span',class_='post-card-tags').text.strip(' \t\n\r')
data["Blog-Title"] = hack[i].find('h2',class_='post-card-title').text.strip(' \t\n\r')
data["Blog-link"] = hack[i].find('a',class_='post-card-image-link').get('href')
data["Blog-link"]= "https://www.freecodecamp.org"+data["Blog-link"]
data["Author"] = hack[i].find('a',class_='meta-item').text.strip(' \t\n\r')
val["blog_data"].append(data)
return val
except Exception as e:
return {"status":False,"error":e}