1

I'm newbie with python and just stuck with my code. Basically I need check Norwegian VATs, in this case I'm using http://w2.brreg.no/enhet/sok/detalj.jsp?orgnr= website, . When I put in VatNo correct one I get:

f = re.findall("Du har oppgitt et ugyldig organisasjonsnummer",page)[0]
IndexError: list index out of range

But if I put incorrect VAT then working well..

This is how my code looks like:

import requests
import re
VatNo = '997814169' #ValidVat

def VatChecker():
    page = requests.get("http://w2.brreg.no/enhet/sok/detalj.jsp?orgnr="+VatNo).text

    x = "Du har oppgitt et ugyldig organisasjonsnummer"
    f = re.findall("Du har oppgitt et ugyldig organisasjonsnummer",page)[0]

    if f==x:
        print ("Invalid VAT")

    else:
        print ("Valid VAT")

VatChecker()

Have you got any ideas where the problem is?

3
  • 1
    Seems that re.findall("Du har oppgitt et ugyldig organisasjonsnummer",page) returned empty list. Commented Dec 17, 2018 at 20:44
  • to expand on ^^, page could be empty or f could be empty. Commented Dec 17, 2018 at 20:46
  • 1
    You should check the return value from re.findall before trying to index into it. If it's an empty list, then of course trying to reference the first element will produce an error. Commented Dec 17, 2018 at 20:47

1 Answer 1

1

It seems like that re.findall("Du har oppgitt et ugyldig organisasjonsnummer",page) returns an empty list, as mentioned by @sashaaero.

To correct the code you can do:

f = re.findall("Du har oppgitt et ugyldig organisasjonsnummer",page)
if (len(f)>0):
    if f==x:
        print ("Invalid VAT")

    else:
        print ("Valid VAT")
Sign up to request clarification or add additional context in comments.

1 Comment

thanks you guys for help!! now it's working perfectly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.