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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:59:04+00:00 2026-06-12T20:59:04+00:00

I parsed an entire HTML file, extracting some URLs with Beautifulsoup module in Python,

  • 0

I parsed an entire HTML file, extracting some URLs with Beautifulsoup module in Python, with this peace of code:

for link in soup.find_all('a'):
    for line in link :
        if "condition" in line :

           print link.get("href")

and i get in the shell a series of links that observe the condition in the if loop:

  • http:// ..link1
  • http:// ..link2
  • .
  • .
  • http:// ..linkn

how can i put in a variable “output” only the first link of this list?

EDIT:

The web page is : http://download.cyanogenmod.com/?device=p970 , the script have to return the first short URL (http://get.cm/…) in the HTML page.

  • 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-12T20:59:06+00:00Added an answer on June 12, 2026 at 8:59 pm

    You can do it with a oneliner:

    import re
    
    soup.find('a', href=re.compile('^http://get.cm/get'))['href']
    

    to assign it to a variable just:

    variable=soup.find('a', href=re.compile('^http://get.cm/get'))['href']
    

    I have no idea what exactly are you doing so i will post the full code from scratch:
    NB! if you use bs4 change the imports

    import urllib2
    from BeautifulSoup import BeautifulSoup
    import re
    
    request = urllib2.Request("http://download.cyanogenmod.com/?device=p970")
    response = urllib2.urlopen(request)
    soup = BeautifulSoup(response)
    variable=soup.find('a', href=re.compile('^http://get.cm/get'))['href']
    print variable
    
    >>> 
    http://get.cm/get/4jj
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I parsed a html page with .findAll (BeautifulSoup) to variable named result .
I need to read a HTML file and search for some tags in it.
So I have this code inside of a class file: Document requestData (String url,
I have parsed a file in tcl and i read the line like that
I have parsed XML file into objects, in which each object has a 1:1
I have order confirmation html emails being parsed by a PHP script. The script
Is it possible to directly store a XML/HTML file inside a SQLite database? I'm
I'm trying to POST the entire HTML for a dynamic table to another page
I'm trying to parse some information from an HTML page. The only problem is
I have a custom html(suppose a.html) file which i am using from the resources

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.