感谢目前为止的一切!还有一个问题,然后有时间擦掉。
我似乎找不到将文本输出到标签的方法,如果可能的话,我希望能够在该标签中显示链接,以便用户可以打开网页。
我已经尝试定义新的程序来执行此操作但是在构建时指定文本时我似乎无法在此之后更改它!我继续得到未定义的变量,因为我明显在错误的地方做这件事!
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.spinner import Spinner
from bs4 import BeautifulSoup
from urllib import request
import sys, traceback
from functools import partial
import re
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.image import Image
class DSFRSapp(App):
def build(self):
self.root = FloatLayout()
i = Image(source='DSFRSLogo.png',
allow_stretch=True,
pos_hint = ({'center_x':0.5, 'y': .25}))
spinner = Spinner(text='Select a Station',
values=('Appledore','Axminster','Bampton','Barnstaple','Bere Alston','Bideford','Bovey Tracey','Braunton','Bridgwater','Brixham','Buckfastleigh','Budleigh Salterton','Burnham on sea','Camels Head','Castle Cary','Chagford','Chard','Cheddar','Chulmleigh','Colyton','Combe Martin','Crediton','Crewkerne','Crownhill','Cullompton','Dartmouth','Dawlish','Exeter Danes Castle','Exeter Middlemoor','Exmouth','Frome','Glastonbury','Greenbank','Hartland','Hatherleigh','Holsworthy','Honiton','Ilfracombe','Ilminster','Ivybridge','Kingsbridge','Kingston','Lundy Island','Lynton','Martock','Minehead','Modbury','Moretonhampstead','Nether Stowey','Newton Abbot','North Tawton','Okehampton','Ottery St Mary','Paignton','Plympton','Plymstock','Porlock','Princetown','Salcombe','Seaton','Shepton Mallet','Sidmouth','Somerton','South Molton','Street','Taunton','Tavistock','Teignmouth','Tiverton','Topsham','Torquay','Torrington','Totnes','USAR','Wellington','Wells','Williton','Wincanton','Witheridge','Wiveliscombe','Woolacombe','Yelverton','Yeovil'),
size_hint=(None, None),
size=(150, 44),
pos_hint = ({'center_x':0.5, 'y': 0.35}))
L = Label(text="Results will display here",
size_hint=(None, None),
pos_hint =({'center_x':0.5, 'y': 0.25}),
size=(150, 44))
self.root.add_widget(spinner)
self.root.add_widget(L)
self.root.add_widget(i)
spinner.bind(text=show_selected_value)
def show_selected_value(spinner, text):
FindIncident(text)
def FindIncident( sStation, *args ):
webpage = request.urlopen("http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35")#main page
soup = BeautifulSoup(webpage)
incidents = soup.find(id="CollapsiblePanel1") #gets todays incidents panel
Links = [] #create list call Links
for line in incidents.find_all('a'): #get all hyperlinks
Links.append("http://www.dsfire.gov.uk/News/Newsdesk/"+line.get('href')) #loads links into Links list while making them full links
n = 0
e = len(Links)
if e == n: #if no links available no need to continue
#output message to label
print("nothing found please try again later")
sys.exit(0)
sFound = False
while n < e: #loop through links to find station
if sFound: #if the station has been found stop looking
sys.exit(0)
webpage = request.urlopen(Links[n]) #opens link in list)
soup = BeautifulSoup(webpage) #loads webpage
if soup.find_all('p', text=re.compile(r'{}'.format(sStation))) == []:#check if returned value is found
#do nothing leaving blank gave error
a = "1" #this is pointless but stops the error
else:
print(soup.find_all('p', text=re.compile(r'{}'.format(sStation))))
WebLink = Links[n]
sFound = True # to avoid un needed goes through the loop process
n=n+1 # moves counter to next in list
if not sFound: #after looping process if nothing has been found output nothing found
print("nothing found please try again later ")
#output to label
return;
if __name__ =="__main__":
DSFRSapp().run()
我应该提一下,我有一个可以显示的徽标 当屏幕重新调整大小时,我会更好地定位小部件 我希望背景颜色为白色,可能带有一个薄的红色边框(只是为了爵士乐)
如果你对这些问题的答案都很好!但目前的优先级是将网络搜索的结果输出到标签,而不是打印到控制台,最好是通过链接(WebLink)
再次感谢,一旦我解决了这些问题,我就打算把它送到我的消防队,作为消防员的一种方式让其他人知道消防队员什么时候可能会完成一次喊叫。但只有一次我可以把它打包到android并且可能让其他人把它移植到ios,因为我没有mac。
答案 0 :(得分:0)
我已将代码更改为pythonic(尤其是循环)。该标签现已连接,但我还没有尝试调试您的网页抓取。你自己必须重新检查它。
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.spinner import Spinner
from bs4 import BeautifulSoup
from urllib import request
import sys, traceback
from functools import partial
import re
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.image import Image
from kivy.properties import ObjectProperty
kv = '''
<IncidentScreen>:
station: stationSpinner
results: resultsLabel
Image:
source:'DSFRSLogo.png'
allow_stretch:True
pos_hint: {'center_x':0.5, 'y': .25}
Spinner:
id: stationSpinner
text:'Select a Station'
values: ('Appledore','Axminster','Bampton','Barnstaple','Bere Alston','Bideford','Bovey Tracey','Braunton','Bridgwater','Brixham','Buckfastleigh','Budleigh Salterton','Burnham on sea','Camels Head','Castle Cary','Chagford','Chard','Cheddar','Chulmleigh','Colyton','Combe Martin','Crediton','Crewkerne','Crownhill','Cullompton','Dartmouth','Dawlish','Exeter Danes Castle','Exeter Middlemoor','Exmouth','Frome','Glastonbury','Greenbank','Hartland','Hatherleigh','Holsworthy','Honiton','Ilfracombe','Ilminster','Ivybridge','Kingsbridge','Kingston','Lundy Island','Lynton','Martock','Minehead','Modbury','Moretonhampstead','Nether Stowey','Newton Abbot','North Tawton','Okehampton','Ottery St Mary','Paignton','Plympton','Plymstock','Porlock','Princetown','Salcombe','Seaton','Shepton Mallet','Sidmouth','Somerton','South Molton','Street','Taunton','Tavistock','Teignmouth','Tiverton','Topsham','Torquay','Torrington','Totnes','USAR','Wellington','Wells','Williton','Wincanton','Witheridge','Wiveliscombe','Woolacombe','Yelverton','Yeovil')
size_hint: None, None
size: (150, 44)
pos_hint: {'center_x':0.5, 'y': 0.35}
on_text: app.show_selected_value()
Label:
id: resultsLabel
text:"Results will display here"
color: [0,0,0,1]
size_hint:(None, None)
pos_hint:{'center_x':0.5, 'y': 0.25}
size: (150, 44)
'''
class IncidentScreen(FloatLayout):
station = ObjectProperty(None)
results = ObjectProperty(None)
class DSFRSapp(App):
def build(self):
Builder.load_string(kv)
fl = IncidentScreen()
return fl
def show_selected_value(self):
incident = self.FindIncident(self.root.station.text)
if incident:
self.root.results.text = incident
else:
self.root.results.text = 'Try again later'
def FindIncident(self, sStation, *args ):
webpage = request.urlopen("http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35")#main page
soup = BeautifulSoup(webpage)
incidents = soup.find(id="CollapsiblePanel1") #gets todays incidents panel
#create list call Links
#get all hyperlinks: loads links into Links list while making them full links
Links = [''.join(["http://www.dsfire.gov.uk/News/Newsdesk/",line.get('href')]) for line in incidents.find_all('a')]
#loop through links to find station
incident = ''
for link in Links:
print('Trying {}'.format(link))
webpage = request.urlopen(link) #opens link in list)
soup = BeautifulSoup(webpage) #loads webpage
#check if returned value is found
incident = soup.find_all('p', text=re.compile(r'{}'.format(sStation)))
print(incident)
break
return incident
if __name__ =="__main__":
DSFRSapp().run()
有一些严重的警告:
额外问题: