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 in Python by Olufemiodubote
• 120 points
98 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
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,230 points
154 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
• 57,640 points
107 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
• 57,640 points
173 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,230 points
522 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
• 57,640 points
132 views
0 votes
2 answers

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

readline function help to  read line in ...READ MORE

answered Jun 21 in Python by sahil
• 540 points
272 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,250 points
103 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,520 points
271 views
+1 vote
8 answers

How to fix this? ValueError: invalid literal for int() with base 10 error in Python

Just for the record: >>> int('55063.000000') Traceback (most recent ...READ MORE

answered Aug 17 in Python by pakainfo
• 200 points
147,588 views