我编写了一个脚本,用于解析某些网站的HTML代码以提取特定数据。我有两个不同的网站,我从中提取这些数据,所以我使用elif语句。这是代码:
import urllib
class city :
def __init__(self, city_name, link) :
self.name = city_name
self.url = link
self.high0 = 0
self.high1 = 0
self.high2 = 0
self.high3 = 0
self.high4 = 0
self.high5 = 0
self.high6 = 0
self.low0 = 0
self.low1 = 0
self.low2 = 0
self.low3 = 0
self.low4 = 0
self.low5 = 0
def retrieveTemps(self) :
filehandle = urllib.urlopen(self.url)
# get lines from result into array
lines = filehandle.readlines()
# (for each) loop through each line in lines
line_number = 0 # a counter for line number
for line in lines:
line_number = line_number + 1 # increment counter
# find string, position otherwise position is -1
position1 = line.rfind('#f2')
if position1 > 0 :
self.high0 = lines[line_number].split('&')[0].split('>')[1] # next line: high
self.low0 = lines[line_number + 10].split('&')[0].split('>')[1] # next line:low
elif position1 < 0 :
position1 = line.rfind('>Overnight')
if position1 > 0 :
self.high0 = lines[line_number + 9].split('&')[0].split(':')[1] # next line: high
self.low0 = lines[line_number + 15].split('&')[0].split(':')[1] # next line:low
当position1 = line.rfind('#f2')时,脚本可以正常工作。但是,当它找不到'#f2'(这只是位于第一个网站的html代码中,而不是第二个网站的html代码中)时,我试图告诉它寻找'&gt;过夜',然后提取数据在':'和'&amp;'之间。 '数据'将始终是一个数字。我想一个问题可能是我试图提取的这个数字的任何一侧都有空格,但我不知道如何解决这个问题。当我运行脚本时,我收到错误:
self.high0 = lines [line_number + 9] .split('&amp;')[0] .split(':')[1]#next line:high “IndexError:列表索引超出范围”
供参考,这是我为第一个网站解析的html代码:
</h3><img src="/weathericons/15.gif" longdesc="#f2" alt="Rain mixed with snow" title="Rain mixed with snow" /><ul>
<li class="high" title="High">3°C</li>
<li class="low"> </li>
<li class="pop"> </li>
</ul>
</div>
并从第二个网站(我收到错误的网站):
<p class="txt-ctr-caps">Overnight<br><br></p>
<p><img src="/images/wtf/medium/nra60.png" width="86" height="86" alt="Rain Likely Chance for Measurable Precipitation 60%" title="Rain Likely Chance for Measurable Precipitation 60%" /></p>
<p>Rain<br>Likely<br></p>
<p class="point-forecast-icons-low">Low: 3 °C</p>
</div>
<div class="one-ninth-first">
<p class="txt-ctr-caps">Thursday<br><br></p>
<p><img src="/images/wtf/medium/ra70.png" width="86" height="86" alt="Rain Likely Chance for Measurable Precipitation 70%" title="Rain Likely Chance for Measurable Precipitation 70%" /></p>
<p>Rain<br>Likely<br></p>
<p class="point-forecast-icons-high">High: 9 °C</p>
</div>
非常感谢任何帮助,谢谢!!
答案 0 :(得分:3)
现在您已经提供了完整的代码: 试试这个
def retrieveTemps(self) :
filehandle = urllib.urlopen(self.url)
lines = filehandle.read() # modified
position1 = lines.rfind('#f2')
if position1 > 0 :
self.high0 = lines[position1:].split('&')[0].split('>')[1] # next line: high
self.low0 = lines[position1+10:].split('&')[0].split('>')[1] # next line:low
elif position1 < 0 :
position1 = lines.rfind('>Overnight')
if position1 > 0 :
self.high0 = lines[position1+9:].split('&')[0].split(':')[1] # next line: high
self.low0 = lines[position1+15:].split('&')[0].split(':')[1] # next line:low
即使这样也无法帮助您尝试使用基本的打印语句来调试代码,例如在处理时使用变量(如行和行)中的内容。