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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:45:29+00:00 2026-06-11T12:45:29+00:00

First off what I am trying to do is ask the user for a

  • 0

First off what I am trying to do is ask the user for a search term. The program then searches yahoo and prints out the link of the first result. Here’s the code I have so far.

from urllib import urlopen

import re, time
from BeautifulSoup import BeautifulSoup


print "What Would You Like to Search For?"

user_input = raw_input('') #Gets Search Term from User



search = "http://search.yahoo.com/search;_ylt=A2KLtaJX_1BQfT4AwX2bvZx4?p=baker&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701" 

new_search = search.replace('baker', user_input)           
content = urlopen( new_search ).read()                       

soupcontent = BeautifulSoup(content)                    


link1 = soupcontent.find(id="link-1")            
print link1

Everything works fine. It takes the user input and searches Yahoo.
The problem I’m having is lets say I searched for ‘dog’

the program would then print something like this:

“a id=”link-1″ class=”yschttl spt” href=”http://www.dog.com/” data-bk=”5101.1>b>Dog/b> Supplies | b>Dog/b> Food, b>Dog/b> Beds, b>Dog/b> wbr>/wbr>Flea Control & More …/a>”

Which Is indeed the first Link on the page. However I would only like it to print out “http://www.dog.com/” Can anyone help me with this?

Thanks.

  • 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-11T12:45:30+00:00Added an answer on June 11, 2026 at 12:45 pm

    BeautifulSoup actually makes this very easy:

    >>> from bs4 import BeautifulSoup
    >>> from urllib2 import urlopen
    >>> 
    >>> url = 'http://search.yahoo.com/search?p=dog'
    >>> content = urlopen(url).read()
    >>> soup = BeautifulSoup(content)
    >>> 
    >>> soup.find(id="link-1")
    <a class="yschttl spt" data-bk="5097.1" href="http://www.dog.com/" id="link-1"><b>Dog</b> Supplies | <b>Dog</b> Food, <b>Dog</b> Beds, <b>Dog</b> <wbr></wbr>Flea Control &amp; More ...</a>
    >>> soup.find(id="link-1").get("href")
    'http://www.dog.com/'
    

    With your request for UTF-8 you’ll probably see

     u'http://www.dog.com/'
    

    instead, the Unicode version, which is fine too.

    Standard warning: be sure to check that Yahoo!’s end-user license permits whatever you want to do, because many licenses rule out certain automated uses.

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

Sidebar

Related Questions

First off I am still new to objective-c and still trying to learn as
First off, I will be talking about some legacy code and we are trying
I am trying to create a very basic sprite image. First off i have
First off, I'd like to apologise for the ludicrous title. I'm not trying to
First off, I'm using version 3.7.1 with a jQuery UI framework theme. I'm trying
First off, it is nice that they are trying to get code completion on
First off, I'm a real beginer at C# so please be gentle. I'm trying
Okay, first off I will attempt to explain what I'm trying to do. I
I have a problem when I use GSON. First off I'm trying to save
First off when I am trying to get my CSS going on my local

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.