Exercise 2 Better solution : Python tutorial 221

///Exercise 2 Better solution : Python tutorial 221

Exercise 2 Better solution : Python tutorial 221

FavoriteLoadingAdd to favorites

This is better solution for exercise 2

source

By |2021-01-04T11:39:16+00:00January 4th, 2021|Python Video Tutorials|25 Comments

25 Comments

  1. Priojit Sen January 4, 2021 at 11:39 am - Reply

    with open(r'.htmlmyTest.html') as rf:

    with open(r'.htmlnew2.html','a') as wf:

    for data in rf.readlines():

    if data.__contains__('<a href'):

    list_urls=data.split(""")

    for link in list_urls:

    if "www" in link:

    wf.write(link+"n")

  2. Samyak Jain January 4, 2021 at 11:39 am - Reply

    we can also use count and a for loop for simplification in previous program!!!

  3. ladva mitesh January 4, 2021 at 11:39 am - Reply

    We can also do as batter solution:
    with open("file.html","r") as html_file:

    with open("output_file.txt","a") as output_file:

    for line in html_file.readlines():

    position = line.find("<a href=")

    for i in range(line.count("<a href=")):

    firstquate_position = line.find('"',position)

    secondquate_position = line.find('"',firstquate_position+1)

    url = line[firstquate_position+1:secondquate_position]

    # print(url)

    output_file.write("==> "+url+"n")

    position = secondquate_position + 1

    Note : This is 100% working try it

  4. JOY DIP January 4, 2021 at 11:39 am - Reply

    Hello sir!! Here May I use page.seek(second_quote) instead of page=page[second_quote: ] ?

  5. Kausik Sharma January 4, 2021 at 11:39 am - Reply

    with open("helpEx1",'r') as f1:

    with open("helpEx2",'a') as f2:

    for line in f1.readlines():

    pos_com=-1

    f_quote=pos_com

    while(line.find(".com",pos_com+1)!=-1):

    pos_com=line.find(".com",pos_com+1)

    f_quote=line.find(""",f_quote+1)

    l=line[f_quote+1:pos_com+4]

    f2.write(f"{l}n")

    f_quote=pos_com+5

    continue

    Sir Check me out ..

  6. AI World January 4, 2021 at 11:39 am - Reply

    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)

  7. DANYAL SHAMSI January 4, 2021 at 11:39 am - Reply

    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")

  8. Rahul Ranjan January 4, 2021 at 11:39 am - Reply

    How about this way?

    import re

    with open('./data.txt') as webpage:

    urls = re.findall(r'(http[^"]+|www[^"]+)', webpage.read())

    for u in urls:
    print(u)

  9. Deepak Singh January 4, 2021 at 11:39 am - Reply

    awesome

  10. Aslam Khan January 4, 2021 at 11:39 am - Reply

    you are great sir

  11. Ketan Bhikadiya January 4, 2021 at 11:39 am - Reply

    nice video ! can you please upload the Source Code Link ?

  12. SHRAVAN PRAJAPATI January 4, 2021 at 11:39 am - Reply

    and how to import numpy in visual studio code ?

  13. We are slave of allah January 4, 2021 at 11:39 am - Reply

    Sir ji Java vi sikhye na.

  14. moqqaa official January 4, 2021 at 11:39 am - Reply

    Sir g, you are best. Love from Pakistan. I have a query. You have described in your playlist description GUI with Tkinter. I want to ask why not PyQt?

  15. adarsha dinda January 4, 2021 at 11:39 am - Reply

    Sir are you complete the writing of python book?

  16. SHRAVAN PRAJAPATI January 4, 2021 at 11:39 am - Reply

    es series m aap hame numpy or pandas k bare m sikhayege??????????????????

  17. 7veNasim XYZ January 4, 2021 at 11:39 am - Reply

    What a wonderful

  18. Jayant Shori January 4, 2021 at 11:39 am - Reply

    when full course will be upladed sir

  19. Rangers Foji January 4, 2021 at 11:39 am - Reply

    appreciate all time

  20. vikas singh Kushwah January 4, 2021 at 11:39 am - Reply

    You are great sir

  21. Abhi Raj January 4, 2021 at 11:39 am - Reply

    Nice video sir

  22. Vanraj Chudasama January 4, 2021 at 11:39 am - Reply

    How to count video duration time in asp.net

  23. Machchhindra Kalingada January 4, 2021 at 11:39 am - Reply

    Nice video sir…jaldi se course khtm kr do

  24. 1PIECEBTC : LEARN TO GROW January 4, 2021 at 11:39 am - Reply

    Aur video kab tak upload hoge

  25. Life Hacks January 4, 2021 at 11:39 am - Reply

    Sir u r great

Leave A Comment

*