-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrape.py
42 lines (28 loc) · 847 Bytes
/
scrape.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
from flask import Flask
import requests, json
from bs4 import BeautifulSoup
app = Flask(__name__)
@app.route("/")
def hello():
html_doc = requests.get("https://www.ndtv.com/topic/women-safety").text
soup = BeautifulSoup(html_doc, 'html.parser')
links_href = soup.select(".fbld a")
photo_href = soup.select(".marr10")
title_text = soup.select(".fbld a strong")
links = []
for link in links_href:
links.append(link.attrs["href"])
titles = []
for title in title_text:
titles.append(title.text)
photos = []
for photo in photo_href:
photos.append(photo.attrs["src"])
data = {}
data['links'] = links
data['photos'] = photos
data['title'] = titles
json_data = json.dumps(data)
return json_data
if __name__ == "__main__":
app.run()