0

I have a web scraper, and I need the number to start at 000000001 as that's how the URL starts, however whenever I run it I get the "leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers" error - which I tried to use but that doesn't solve my issue.

Is there any way around this?

Excerpt:

results = []
START = 000000001
LIMIT = 100
END = START + LIMIT + 1
row = 1

for i in range(START, END):
    URL = f"{url}{i}"
    result = parse(URL)
    if not result:
        print("this url doesn't exist: ", URL)
        continue
    for column_number, column_value in enumerate(result):
        worksheet.write(row, column_number, column_value)
    row += 1
Aedam
  • 141
  • 1
  • 9
  • 1
    Do you want to _read_ a number with leading zeros, or rather _print_ one (or insert one into a string, e.g. URL)? – tobias_k Oct 22 '20 at 14:38
  • You need a *string*, not an integer, that contains a particular representation of the integer 1. – chepner Oct 22 '20 at 14:39
  • @jordanm I had seen that prior to this but I didn't think that solved my issue. Am I misunderstanding that thread? – Aedam Oct 22 '20 at 14:41
  • BTW, in case you are wondering, leading zeros in integer literals are disallowed to avoid confusion with Octal numbers. E.g. Python 2 would just silently read `042` as `34` (4x8+2). – tobias_k Oct 22 '20 at 14:48

1 Answers1

4

You need str values that contain fixed-width representations of integers.

start = 1
limit = 100
end = start + limit + 1
row = 1


for i in range(start, end):
    url = f'{url}{i:09}'
    ...

Following the :, the 0 indicates the string should be padded with leading zeros to fill a string with length 9.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you so much! I wasn't sure what the thread meant by make the number into a string, but this makes sense now. I'll mark yours as the answer – Aedam Oct 22 '20 at 14:42