字符串搜索返回none或[] [英] string search returns none or []
问题描述
我有我想打开一个特定的地方,搜索特定的名字多个URL,但我才刚刚返回无或[]。
I have several urls that i want to open to a specific place and search for a specific name but I'm only getting None returned or [].
我已搜查,但不能看到一个答案是有关我的code。
I have searched but cannot see an answer that is pertinent to my code.
from bs4 import BeautifulSoup
from urllib import request
webpage = request.urlopen("http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35")
soup = BeautifulSoup(webpage)
incidents = soup.find(id="CollapsiblePanel1")
Links = []
for line in incidents.find_all('a'):
Links.append("http://www.dsfire.gov.uk/News/Newsdesk/"+line.get('href'))
n = 0
e = len(Links)
while n < e:
webpage = request.urlopen(Links[n])
soup = BeautifulSoup(webpage)
station = soup.find(id="IncidentDetailContainer")
#search string
print(soup.body.findAll(text='Ashburton'))
n=n+1
我知道在页面上找到的最后一个环节了。
I know its in the last link found on the page.
在此先感谢您的任何想法或意见
Thanks in advance for any ideas or comments
推荐答案
如果您的输出中是[]只是,这意味着你的输出是一个数组。你必须设置索引,然后=>变量[指数]。
If your ouput is "[]" only, it means your output is an array. You have to set the index then => variable[index].
试试这个
print(soup.body.findAll(text='Ashburton')[0])
...其中将其存储到一个变量第一会更容易:
...where storing it into a variable first would be easier:
search = soup.body.findAll(text='Ashburton')
print(search[0])
这将带给你找到的第一个项目。
This will bring you the first found item.
有关打印找到的所有项目,你可以去
For printing all found items you could go
search = soup.body.findAll(text='Ashburton')
foreach(entry in search)
print(entry)
请注意这是更pseude- code,而不是工作的例子。我真的不知道beautifulsoap。的
Notice this is more pseude-code instead of a working example. I really dont know beautifulsoap.
这篇关于字符串搜索返回none或[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!