Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:07:40+00:00 2026-06-09T09:07:40+00:00

I have built a webscraper with a for-loop. I don’t know why, but it

  • 0

I have built a webscraper with a for-loop. I don’t know why, but it returns an url (which is what I want it to return), and then before fetching the next url in the list, it returns a NoneType object. Other than making the script slower, it’s not a big deal, if it wasn’t because I can’t get it to print more than the first url.

from BeautifulSoup import BeautifulSoup
from mechanize import Browser
br = Browser()
page = br.open("https://bdkv2.borger.dk/foa/Sider/default.aspx?fk=22&foaid=11541520")
html = page.read()
soup = BeautifulSoup(html)
link = soup.findAll('a')
kommunelink = link[21:116]
for kommune in kommunelink:
    kommuneside = br.open(kommune['href'])
    html2 = kommuneside.read()
    soup2 = BeautifulSoup(html2)
    hjemmesidelink = soup2.find('a', id='_uscAncHomesite')
    print hjemmesidelink['href']

This way my output is like this:

http://www.albertslund.dk

Traceback (most recent call last):
File "C:\Users\kba\Desktop\kommuneskraber.py", line 14, in <module>
print hjemmesidelink['href']
TypeError: 'NoneType' object has no attribute '__getitem__'

I’ve tried messing around with stuff like: If variable == specific class, then print, but that doesn’t work. Example:

If hjemmesidelink['href'] == <class 'BeautifulSoup.Tag'>:
    print hjemmesidelink['href']

if hjemmesidelink.class == BeautifulSoup.Tag:
    print hjemmesidelink['href']

Any idea how it should be? Or maybe even better, any idea where/why my script fetches a ‘NoneType’ object every second time it iterates through the loop? Thanks a bunch.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:07:41+00:00Added an answer on June 9, 2026 at 9:07 am

    this is not a complete answer, but if you look at the comments this will answer just the part about not producing an error.

    at this part of the code:

    print hjemmesidelink['href']
    

    replace with:

    if hjemmesidelink:
        print hjemmesidelink['href']
    

    the if hjemmesidelink: checks if hjemmesidelink has a value, if it does, then it prints it, if not, it will continue the loop.

    my results:

    >>> 
    http://www.albertslund.dk
    http://www.alleroed.dk
    http://www.assens.dk
    http://www.ballerup.dk
    http://www.billund.dk
    http://www.brk.dk
    http://www.brondby.dk
    http://www.broenderslev.dk
    http://www.dragoer.dk
    

    and counting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built and app which does random stuff and I want to collect
I have built an R package but I do not want my users to
I have built a site which requires user to login. Now I have decided
I have built the UI for my Google Chrome Extension, but I am trying
I have built a site using php and want to try keep it one
I have built ICS source using make -j4, then I have modified the Music
I have built a Fiddle on JSFiddle which retrieves an OAuth access token via
I have built a function to create a dictionary and return it. This function
I have built a music player which loads songs from a database in a
I have built a table which on of it's row is like so <tr

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.