Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- import requests
- from kivy.uix.textinput import TextInput
- from kivy.uix.label import Label
- from kivy.app import App
- from kivy.uix.floatlayout import FloatLayout
- from kivy.uix.button import Button
- from kivy.uix.scrollview import ScrollView
- from kivy.uix.popup import Popup
- class RootWidget(FloatLayout):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.result = ''
- self.label_intro = Label(text='Webscrapper', size_hint=(None, None), size=(150,50),
- color=(0,0,1,1), pos_hint={'top':1, 'center_x':0.5})
- self.label_url = Label(text='URL:', size_hint=(None, None), pos_hint={'top':0.90})
- self.txtin_url = TextInput(text='',
- size_hint=(None, None), size=(500,200), pos_hint={'top':0.9, 'x':0.15})
- self.btn = Button(text='scrape text', size_hint=(None, None), size=(180,60), pos_hint={'top':0.695, 'x':0.150},
- on_press=self.scrape)
- self.btn3 = Button(text='scrape link',
- size_hint=(None, None), size=(180,60), pos_hint={'top':0.695, 'x':0.60}, on_press= self.scrape_link)
- self.sv = ScrollView(pos_hint={'top':0.599, 'center_x':0.50}, size_hint=(None, None),
- width=700, height=600)
- self.lab = TextInput(text='', size_hint_y=None, height=170000)
- self.popup1 = Popup(title='Error', content=Label(text='pls enter url'),
- size_hint=(None, None), size=(300,300), pos_hint={'top':0.68, 'center_x':0.50})
- self.btn2 = Button(text='Clear',
- size_hint=(None,None), size=(130,50),
- pos_hint={'top':0.06, 'center_x':0.50},
- on_press=self.clear)
- self.sv.add_widget(self.lab)
- self.add_widget(self.label_intro)
- self.add_widget(self.label_url)
- self.add_widget(self.txtin_url)
- self.add_widget(self.btn)
- self.add_widget(self.btn3)
- self.add_widget(self.sv)
- self.add_widget(self.btn2)
- def scrape(self, *args):
- #self.popup1.open()
- try:
- a = self.txtin_url.text
- a = str(a)
- r = requests.get(a).text
- soup = BeautifulSoup(r, 'html.parser')
- matches = soup.find_all(['h1', 'h2',
- 'h3', 'span', 'strong', 'em', 'p', 'section'])
- except:
- self.popup1.open()
- else:
- for match in matches:
- self.result += match.text + '\n'
- self.lab.text = self.result
- def clear(self, *args):
- self.lab.text = ''
- self.result = ''
- def scrape_link(self, *args):
- try:
- a = self.txtin_url.text
- a = str(a)
- r = requests.get(a).text
- soup = BeautifulSoup(r, 'html.parser')
- links = soup.find_all('a')
- except:
- self.popup1.open()
- else:
- for link in links:
- self.result += link.text + '\n'
- self.result += link.get('href') + '\n'
- self.lab.text = self.result
- class Main(App):
- def build(self, *args):
- return RootWidget()
- app = Main()
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement