23 thoughts on “Exercise 2 Better solution : Python tutorial 221

  1. More enhanced way

    #program to extract n no. of links in one line links
    target = r'B:Python CourseCh18_Working_with_filesCh18_Ex2html.txt'
    extract = r'B:Python CourseCh18_Working_with_filesCh18_Ex2extract.txt'

    def url_finder(link):
    if "href" in link:
    comma_split = link.split('"') # ' " '
    return [each for each in comma_split if each[-4: ].lower() == ".com" ]

    def transfer(target, extract):
    with open(target) as rf:
    with open(extract, 'w') as wf:
    for line in rf.readlines():
    contain_url = url_finder(line)
    if contain_url != None:
    for extractedLink in contain_url:
    wf.write(f"n> {extractedLink}")
    wf.close()
    rf.close()

    transfer(target, extract)

  2. Dear sir, please help. I thought this should work, but it isn't. Please tell if there is some error. It returns the result as the previous code.

    with open('Webpage.htm', 'r') as rf:
    with open('output.txt', 'w') as wf:
    for line in rf.readlines():
    if 'a href' in line:
    l1 = line.split(""")
    for l in l1:
    if 'www' in l:
    wf.write(f"{l}n")

Leave a Reply

Your email address will not be published. Required fields are marked *