简体   繁体   中英

print all urls - python

I already created a get_url function that retrieves one url, I am now trying to call that function to define a different one, print_all_links(s), that prints all urls from an inputted string. This is what I have so far, it prints the correct amount of url's but they are just the first url repeated, and not on one line, please help

def get_url(s):
    #s = s.replace(" ", "")
    b = s.find('<a href=')
    if b != -1:
        return s[b + 9 : b + 9 + s[b + 9::].find('"')]
    else:
        return b

#prints all URls in HTML input
def print_all_links(s):
    i = s.count('<a href=')
    n = i
    x = ''
    while n > 0:
        x += get_url(s)
        print(x)
        s = s.replace(x, '')
        n = n-1

sample input: print_all_links(' University of Illinois at Chicago |

prints: https://uic.edu https://uic.edu

try this

def get_url(s):
    #s = s.replace(" ", "")
    b = s.find('<a href=')
    if b != -1:
        return s[b + 9 : b + 9 + s[b + 9::].find('"')]
    else:
        return b

#prints all URls in HTML input
def print_all_links(s):
    i = s.count('<a href=')
    print(i)
    n = i
    x = ''
    while n > 0:
        x = get_url(s)
        print(x)
        s = s.replace('<a href="' + x, '')
        n = n-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM