简体   繁体   English

打印所有网址-python

[英]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. 我已经创建了一个get_url函数来检索一个URL,我现在试图调用该函数来定义一个不同的print_all_links(s),该函数可以打印输入字符串中的所有URL。 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 这就是到目前为止,它可以打印正确数量的url,但是它们只是重复的第一个url,而不是一行,请帮助

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 | 输入示例:print_all_links(' 伊利诺伊大学芝加哥分校 |

prints: https://uic.edu https://uic.edu 打印: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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