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 9115345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:24:32+00:00 2026-06-17T04:24:32+00:00

I am using the below code to scrape over XFN content from web page

  • 0

I am using the below code to scrape over XFN content from web page http://ajaxian.com
but I am getting undefined variable error:

My code is as follows:

'''
Created on Jan 11, 2013

@author: Somnath
'''
# Scraping XFN content from a web page
# -*-coding: utf-8 -*-

import sys
import urllib2
import HTMLParser
from BeautifulSoup import BeautifulSoup

# Try http://ajaxian.com
URL = sys.argv[0]

XFN_TAGS = set([
            'colleague',
            'sweetheart',
            'parent',
            'co-resident',
            'co-worker',
            'muse',
            'neighbor',
            'sibling',
            'kin',
            'child',
            'date',
            'spouse',
            'me',
            'acquaintance',
            'met',
            'crush',
            'contact',
            'friend',
            ])


try:
    page = urllib2.urlopen(URL)
except urllib2.URLError:
    print 'Failed to fetch ' + item

try:
    soup = BeautifulSoup(page)
except HTMLParser.HTMLParseError:
    print 'Failed to parse ' + item

anchorTags = soup.findAll('a')

for a in anchorTags:
    if a.has_key('rel'):
        if len(set(a['rel'].split()) & XFN_TAGS) > 0:
            tags = a['rel'].split()
            print a.contents[0], a['href'], tags

I have two try blocks in my code and it is giving an error undefined variable : item. If I want to re-include the try-except blocks, should I give a blank definition of variable, item outside the try blocks?

P.S: Please note that is a standard code followed from a book. And I expect that they would not have made such a trivial mistake. Am I getting something wrong here ?

  • 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-17T04:24:34+00:00Added an answer on June 17, 2026 at 4:24 am

    Assuming that you want to print the URL that failed to load, try changing it to print 'Failed to fetch ' + URL. You aren’t actually defining item anywhere, so Python doesn’t know what you mean:

    try:
        page = urllib2.urlopen(URL)
    except urllib2.URLError:
        print 'Failed to fetch ' + URL
    

    And in your second block, change item to URL as well (assuming the error you want to display shows the URL and not the content).

    try:
        soup = BeautifulSoup(page)
    except HTMLParser.HTMLParseError:
        print 'Failed to parse ' + URL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the below code to scrape over XFN content from web page
i using below code to get a picture from URL: URL url=new URL(http://www.google.com/images/logos/ps_logo2.png); InputStream
I am using below code to display web page Uri uri = Uri.parse(http://myurl); Intent
I am using below code to get a page: $.get('http://example.com/page1.html', function (data) { });
I am trying to scrap some content from a website but the code below
I am using below reffered code to scrape some item names from a site:
I am using below code to generate photo gallery from a folder. How can
I am using below code for Embed MP3 Audio Files In Web Pages, <embed
I am using below code to send a file via blue-tooth from one device
I am using below code to Add SummaryLinkWebPart to a Page and also adding

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.