I need parse HTML doc with sports odds. parsing finish, I get data what I need but in end of script I get error:

line 36, in <module>
print(line[0].text.split(' ',1), line[1].text.split(' ',1))
IndexError: list index out of range

Example HTML:

<div class="Section">

<div id="ctl00_Main_ctl00_ctl78_ctl00_SctDescDiv" class="Header">Natsuho Arakawa v Mandy Wagemaker</div><div id="ctl00_Main_ctl00_ctl78_ctl01_Lines" class="Lines">
<div id="ctl00_Main_ctl00_ctl78_ctl01_ctl00_LineDiv" class="Line"><input type="checkbox" id="77" name="77a" value="pt=N#o=5/2#f=46064591#fp=549590497#so=0#ln=#c=13#" onclick="javascript:stopInPlayRefresh();">3.50 Natsuho Arakawa</div>
<div id="ctl00_Main_ctl00_ctl78_ctl01_ctl01_LineDiv" class="Line"><input type="checkbox" id="78" name="78a" value="pt=N#o=2/7#f=46064591#fp=549590498#so=0#ln=#c=13#" onclick="javascript:stopInPlayRefresh();">1.28 Mandy Wagemaker</div>

My code:

for x in soup.find_all('div', ['Lines']):
    line = x.find_all('div')
    print(line[0].text.split(' ',1), line[1].text.split(' ',1))

Results:

['1.33', 'Elena Vesnina'] ['3.25', 'Bojana Jovanovski']
['2.75', 'Irina-Camelia Begu'] ['1.40', 'Kaia Kanepi']
['2.75', 'Polona Hercog'] ['1.40', 'Lucie Safarova']
['1.44', 'Svetlana Kuznetsova'] ['2.62', 'Maria Teresa Torro-Flor']

Everything works but I do not understand the error. What is wrong ? help me please. Thank you

有帮助吗?

解决方案

This code modification shall reveal, that in some cases you are gettign line not having 2 elements.

for x in soup.find_all('div', ['Lines']):
    line = x.find_all('div')
    if len(line) => 2:
        print(line[0].text.split(' ',1), line[1].text.split(' ',1))
    else:
        print "line is too short", line
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top