Parse Xpath from Int

Asked

Viewed 53 times

0

I have a scrapy running the for to bring the day and link to something. Ex:

t_day = div.xpath('.//a/text()').extract_first()
a_day = div.xpath('.//a/@href').extract_first()
day = int(t_day)
if day > last_day:
    print(t_day, a_day)

And in execution returns the error:

in parse
day = int(t_day)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

What I need to do to solve the execution?

  • What is the content of the variable t_day?

  • content executes values. xpath

  • I know, but what is the value that is assigned at the moment that it tries to parse the int(t_day), because the error that is giving is that the content is not numerical.

  • Ta going value Nonetype, I believe it is passing type Null same

  • You found the problem, null cannot be converted to integer.

  • how do I treat the comparison null for integer ?

  • You can leave a default value for it: int(0 if a is None else t_day) you can replace 0 by the value you want to be default.

  • Thanks man, I executed here and it worked ! vlw

Show 3 more comments

1 answer

0


This error happens because the value of the variable t_day is null, you can use an if ternary to solve the problem, returning a default value if the variable is null.

int(0 if a is None else t_day)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.