How do I fix this Set function in python

0 votes
I have the following code below I am working on, in order to get the relative links to absolute links, duplicate links out and unique links as my output result into CSV file:-

    import requests
    from bs4 import BeautifulSoup
    import csv

    page = 'https://www.census.gov/programs-surveys/popest.html'
    r = requests.get(page)
    raw_html = r.text
    soup = BeautifulSoup(raw_html, 'html.parser')

    links = soup.find_all("a")
    print('Number of links retrieved: ', len (links))

    Myset = set()
    for link in links:
    hrefs = str(link.get("href"))
    if hrefs.startswith('None'):
    elif hrefs.startswith("#http"):
    Myset.add(hrefs[1:])
    elif hrefs.startswith('#'):
    elif hrefs.startswith('/'):
    Myset.add('https://www.census.gov/programs-surveys/popest.html' + hrefs)
    elif hrefs.endswith('.gov'):
    Myset.add(hrefs + '/')
    else:
    Myset.add(hrefs)

    f = open('Mytest.csv', 'w')
    writer = csv.writer(f, delimiter='', lineterminator='\r')
    Mylist = ()
    ctr = 0
    for x in Myset:
    Mylist.append(x)
    if not Mylist:
    else:
    writer.writerow(Mylist)
    del Mylist(:)
     ctr = 1

But I kept getting errors message like this ones below:

  File "<tokenize>", line 6
    elif hrefs.startswith("#http"):
    ^
IndentationError: unindent does not match any outer indentation level

                 &

      File "<ipython-input-14-24882d8efa93>", line 5
    elif hrefs.startswith("#http"):
       ^
SyntaxError: invalid syntax

 I am stuck and getting frustrated.
May 20, 2020 in Python by Olufemiodubote
• 120 points
970 views

Hey, @There,

You just have to remove the spacing across all the lines of code in that specific set and space it again with the tabs correctly. This will fix your problem.

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,770 points
2,393 views
0 votes
1 answer

Pytesseract : "TesseractNotFound Error: tesseract is not installed or it's not in your path", how do I fix this?

Based on my recent experience with this ...READ MORE

answered Feb 16, 2022 in Python by Soham
• 9,710 points
37,699 views
0 votes
2 answers

How do I copy a file in python?

copy a file in python  from shutil ...READ MORE

answered Mar 27, 2019 in Python by rajesh
• 1,270 points
1,314 views
0 votes
1 answer

How do I copy a file in python?

Use the shutil module. copyfile(src, dst) Copy the contents ...READ MORE

answered Jul 31, 2018 in Python by Priyaj
• 58,020 points
982 views
0 votes
1 answer

Writing unit tests in Python: How do I start?

If you're brand new to using unittests, ...READ MORE

answered Sep 18, 2018 in Python by Priyaj
• 58,020 points
923 views
0 votes
2 answers

How do I connect to a MySQL Database in Python?

connect mysql database with python import MySQLdb db = ...READ MORE

answered Mar 28, 2019 in Python by rajesh
• 1,270 points
1,892 views
0 votes
1 answer

How can I build a recursive function in python?

I'm wondering whether you meant "recursive". Here ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,020 points
1,147 views
0 votes
1 answer

In Python, how do I read a file line-by-line into a list?

with open(fname) as f:     content = f.readlines() # you ...READ MORE

answered Oct 9, 2018 in Python by SDeb
• 13,300 points
1,799 views
0 votes
1 answer

How do I append one string to another in Python?

If you only have one reference to ...READ MORE

answered Oct 22, 2018 in Python by SDeb
• 13,300 points
744 views
0 votes
1 answer

In NumPy how do I get the maximum of subsets? Python

You can use np.maximum.reduceat: >>> _, idx = np.unique(g, ...READ MORE

answered Nov 9, 2018 in Python by Nymeria
• 3,560 points
1,597 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP