forked from whohet/Web-Scrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewsscrap.py
54 lines (43 loc) · 1.92 KB
/
newsscrap.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
import requests
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtCore import QCoreApplication
from gui import Ui_MainWindow
from bs4 import BeautifulSoup
import sys
class MainWindow(QMainWindow, Ui_MainWindow):
switch_window = QtCore.pyqtSignal()
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.Search_Button.clicked.connect(self.Search_News)
self.Title.setText(QCoreApplication.translate("MainWindow",
"<html><head/><body><p align=\"center\"><span style=\" font-size:11pt; font-weight:600;\">Search News of Any city!</span></p></body></html>"))
def Search_News(self):
city = self.Search_item.toPlainText()
city += '-news'
url = 'https://www.ndtv.com/' + city
# creating an HTTP request
page = requests.get(url)
if page.status_code == 404:
page = requests.get('https://www.ndtv.com/others-news')
soup = BeautifulSoup(page.content, 'html.parser')
items = soup.find_all('div', class_='news_Itm')
self.Output.setText("")
for news in items:
heading = news.find('h2', class_='newsHdng')
if heading != None:
heading = heading.find('a').text
self.Output.append("Heading: "+heading.strip()+'\n')
postedBy = news.find('span', class_='posted-by')
if postedBy != None:
postedBy = postedBy.text
self.Output.append("Posted By:n"+postedBy.strip()+'\n')
content = news.find('p', class_='newsCont')
if content != None:
content = content.text
self.Output.append("Content: "+content.strip()+'\n')
self.Output.verticalScrollBar().setSliderPosition(0)
self.show()
def News(self):
self.switch_window.emit()