0

I am trying to download files from a website. The URL is in HTML of the page, I am able to find the right one, but the various videos are in different frame per second (fps). I had multiple try functions as seen here but it was difficult to follow, so I tried the loop function seen here.

This is what I have:

    import re
    for [j] in range(23,24,25,26,27,28,29,30,31):
        try:

            result = re.search('"width":1280,"mime":"video/mp4","fps":[j],"url":(.*),', s)

            extractedword=result.group(1)

            commapos=extractedword.find(",")

            link=extractedword[:commapos].replace('"',"")
        except:
            pass
        print(title)
        print(link)

The output message states range expected at most 3 arguments, got 9

Any advice how I can search for the correct URL? I've been trying to get this done for days. Thank you!

EDIT: I should add the URL for one title only exists in one FPS at the set resolution. The various titles exist in a variety of FPS, but each title is only available in one FPS for the required resolution. Some of the solutions are returned "download error retrying" in a loop.

OnMyWay
  • 1
  • 1

3 Answers3

0

Use this code instead:

import re

s = # Put what s equals here

for j in range(23, 32):
    try:

        result = re.search('"width":1280,"mime":"video/mp4","fps":[j],"url":(.*),', s)

        extractedword = result.group(1)

        commapos = extractedword.find(",")

        link = extractedword[:commapos].replace('"', "")
    except:
        pass
    else:
        print(title) # Also make sure to define title somewhere
        print(link)
abhigyanj
  • 2,355
  • 2
  • 9
  • 29
  • The return now states download error retrying in a loop. I edited my question to be more specific, sorry if I wasn't clear at first – OnMyWay Oct 18 '20 at 04:18
0

range takes three arguments: start,stop,step. So instead try this:

# other variables
import re
for j in range(23,31):
  try:
    result = re.search('"width":1280,"mime":"video/mp4","fps":[j],"url":(.*),', s)
    extractedword=result.group(1)
    commapos=extractedword.find(",")
    link = extractedword[:commapos].replace('"',"")
  except:
    pass
  else:
    print(title)
    print(link)
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • The return now states download error retrying in a loop. I edited my question to be more specific, sorry if I wasn't clear at first – OnMyWay Oct 18 '20 at 04:18
0

You really do not need range or even a loop here. There are many issues,

  1. Not calling range correctly. start, stop, step
  2. Never calling j as part of your regex

Given the issue, you are cautious that frame rates change and the regex may fail to match if that occurs. A simple, more elegant solution here is to just update your regex to match all the possible frame rates that may be present.

2[3-9]|3[0-1]

The above regex will match all numbers from 23 to 31. If we specify this as a capturing group, we can also access this via our search later if we want to store the frame rate.

import re

s = '"width":1280,"mime":"video/mp4","fps":25,"url":test.com'

result = re.search('"width":1280,"mime":"video\/mp4","fps":(2[3-9]|3[0-1]),"url":(.*)', s)

result.group(1)
#'25'
result.group(2)
#'test.com'

From here you can proceed to modify the output however you want. For a more in depth explanation of the regex you can see the calculation steps at regex101

PacketLoss
  • 5,561
  • 1
  • 9
  • 27
  • The return now states download error retrying in a loop. I edited my question to be more specific, sorry if I wasn't clear at first – OnMyWay Oct 18 '20 at 04:16