python xpath IndexError: list index out of range

3.6k views Asked by At

I am grabbing a value from url source page by xpath. but this is not existed. so I want to pass the requests and try to grab that value again: what I tried:

import requests
from lxml import html
url='http://www.example.com'
cont=requests.get(url)
tree=html.fromstring(cont)
out=tree.xpath('...')[0]

When I run it I have the following Error:

IndexError: list index out of range

How can I request for grabbing value again in this case?

update

I know this error means xpath doesn't exist. so I want to request to that url to grab xpath again.

1

There are 1 answers

0
Vincent Beltman On BEST ANSWER

Use Try Except

import requests
from lxml import html

def do_get(xpath): # OR URL What ever you need
    url='http://www.example.com'
    cont=requests.get(url)
    tree=html.fromstring(cont)
    out=tree.xpath(xpath)[0]

try:
    do_get('....')
except:
    do_get('....')

Or if you wanna do it forever until you get the right one:

def do_get(xpath):
    url='http://www.example.com'
    cont=requests.get(url)
    tree=html.fromstring(cont)
    out=tree.xpath(xpath)[0]

while True:
    try:
        do_get('....')
        break
    except:
        pass