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

  • SEARCH
  • Home
  • 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 7018925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:06:55+00:00 2026-05-27T23:06:55+00:00

I am trying to parse the given below html code using lxml.html and using

  • 0

I am trying to parse the given below html code using lxml.html and using CSSSelector instead of XPath.

link = doc.cssselect('html body div.results dl dt a)

the above code is giving me content-1 and content-2 as output but my desired output is link 1 link 2. So I replaced my code with

link = doc.cssselect('html body div.results dl dt a[href]')

but still am getting the same output. So my question is what’s the proper CSS selector to get href attribute.

             <div class = "results">
                     <div> some tags here </div>
                        <dl> 
                              <dt title = "My Title 1" style = "background: transparent url('/img/accept.png') no-repeat right center">
                              <a href = "/link 1"> content-1</a> 
                              </dt>
                       </dl>

                      <dl>
                             <dt title = "My Title 2" style = "background: transparent url('/img/accept.png') no-repeat right center">
                             <a href = "/link 2">content-2</a>
                             </dt>
                     </dl>
            </div>
  • 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-05-27T23:06:56+00:00Added an answer on May 27, 2026 at 11:06 pm

    I believe you cannot get the attribute value through CSS selectors. You should get the elements…

    >>> elements = doc.cssselect('div.results dl dt a')
    

    …and then get the attributes from them:

    >>> for element in elements:
    ...     print element.get('href')
    ... 
    /link 1
    /link 2
    

    Of course, list comprehensions are your friends:

    >>> [element.get('href') for element in elements]
    ['/link 1', '/link 2']
    

    Since you cannot update properties of attributes in CSS, I believe there is no sense on getting them through CSS selectors. You can “mention” attributes in CSS selectors to retrieve only to match their elements. However, is is just cogitation and I may be wrong; if I am, please someone correct me 🙂 Well, @Tim Diggs confirms my hypothesis below 🙂

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

Sidebar

Related Questions

I am trying to parse an RSS feed with feedparser. The below code snipped
I am trying to pass the XML code below, in the first instance using
Hey, I'm trying to use VTD-XML to parse XML given to it as a
Trying to parse an HTML document and extract some elements (any links to text
Im trying to parse the string located in /proc/stat in a linux filesystem using
I'm trying to parse an INI file using C++. Any tips on what is
I am trying to parse a csv file where my data is given in
I am trying to parse a bit of data from an HTML file, but
I am trying to parse a given textfile, but so far, my program does
I'm trying to get google search results using HTML Agility Pack, but I am

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.