【发布时间】:2014-12-21 01:50:40
【问题描述】:
我已经编写了返回这个的代码:
<div id="IncidentDetailContainer"><p>The Fire Service received a call reporting a car on fire at the above location. One fire appliance from Ashburton attended.</p><p>Fire crews confirmed one car well alight and severley damaged by fire. The vehicle was extinguished by fire crews using two breathing apparatus wearers and one hose reel jet. The cause of the fire is still under investigation by the Fire Service and Police.</p><p> </p><p> </p></div>
我想搜索它并找到“Ashburton”部分,但到目前为止,无论我使用什么,我都没有返回或 []。
我的问题是:这是一个可以搜索的普通字符串(我做错了什么)还是因为我从网页源代码中得到它,我无法以正常方式搜索它?
应该很简单,我知道,但我还是没有!
from bs4 import BeautifulSoup
from urllib import request
import sys, traceback
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)
if e == n:
print("No Incidents Found Please Try Later")
sys.exit(0)
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
仅供参考,如果网页今天没有任何事件,它不会搜索任何内容(显然)这就是我包含返回的字符串的原因,所以如果你运行它却什么也没得到,这就是原因。
【问题讨论】:
-
您要查找包含“Ashburton”或标签的文本吗?
-
我得到了包含名称的标签,但我想在标签内搜索,因为名称会改变,我想最终搜索不同的名称
标签: string search beautifulsoup webpage