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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:54:07+00:00 2026-06-18T08:54:07+00:00

I have a webpage that I am currently parsing using BeautifulSoup but it is

  • 0

I have a webpage that I am currently parsing using BeautifulSoup but it is quite slow so I have decided to try lxml as I read it is very fast.

Anyway, I am struggling to get my code to iterate over the section I want, not sure how to use lxml and I can’t find clear documentation on it.

Anyway, here is my code:

import urllib, urllib2
from lxml import etree

def wgetUrl(target):
    try:
        req = urllib2.Request(target)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        outtxt = response.read()
        response.close()
    except:
        return ''
    return outtxt

newUrl = 'http://www.tv3.ie/3player'

data = wgetUrl(newUrl)
parser = etree.HTMLParser()
tree   = etree.fromstring(data, parser)

for elem in tree.iter("div"):
    print elem.tag, elem.attrib, elem.text

This returns all the DIV’s but how do I specify to only iterate through dev id=’slider1′?

div {'style': 'position: relative;', 'id': 'slider1'} None

This does not work:

for elem in tree.iter("slider1"):

I know this is probably a dumb question but I can’t figure it out..

Thanks!

* EDIT **

With your help adding this code I now have the output below:

for elem in tree.xpath("//div[@id='slider1']//div[@id='gridshow']"):
    print elem[0].tag, elem[0].attrib, elem[0].text
    print elem[1].tag, elem[1].attrib, elem[1].text
    print elem[2].tag, elem[2].attrib, elem[2].text
    print elem[3].tag, elem[3].attrib, elem[3].text
    print elem[4].tag, elem[4].attrib, elem[4].text

Output:

a {'href': '/3player/show/392/57922/1/Tallafornia', 'title': '3player | Tallafornia, 11/01/2013. The Tallafornia crew are back, living in a beachside villa in Santa Ponsa, Majorca. As the crew settle in, the egos grow bigger than ever and cause tension'} None
h3 {} None
span {'id': 'gridcaption'} The Tallafornia crew are back, living in a beachside vill...
span {'id': 'griddate'} 11/01/2013
span {'id': 'gridduration'} 00:27:52

That is all brilliant but I am missing a part of the a tag above. Would the parser be not handling the code correctly?

I’m not getting the following:

<img alt="3player | Tallafornia, 11/01/2013. The Tallafornia crew are back, living in a beachside villa in Santa Ponsa, Majorca. As the crew settle in, the egos grow bigger than ever and cause tension" src='http://content.tv3.ie/content/videos/0378/tallaforniaep2_fri11jan2013_3player_1_57922_180x102.jpg' class='shadow smallroundcorner'></img>

Any ideas why It doesn’t pull this?

Thanks again, very helpful posts..

  • 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-18T08:54:08+00:00Added an answer on June 18, 2026 at 8:54 am

    You can use an XPath expression as follows:

    for elem in tree.xpath("//div[@id='slider1']"):
    

    Example:

    >>> import urllib2
    >>> import lxml.etree
    >>> url = 'http://www.tv3.ie/3player'
    >>> data = urllib2.urlopen(url)
    >>> parser = lxml.etree.HTMLParser()
    >>> tree = lxml.etree.parse(data,parser)
    >>> elem = tree.xpath("//div[@id='slider1']")
    >>> elem[0].attrib
    {'style': 'position: relative;', 'id': 'slider1'}
    

    You need to better analyse the contents of the page you are processing (a good way to do this is to use Firefox with the Firebug add-on).

    The <img> tag you are trying to obtain is actually a child of the <a> tag:

    >>> for elem in tree.xpath("//div[@id='slider1']//div[@id='gridshow']"):
    ...    for elem_a in elem.xpath("./a"):
    ...       for elem_img in elem_a.xpath("./img"):
    ...          print '<A> HREF=%s'%(elem_a.attrib['href'])
    ...          print '<IMG> ALT="%s"'%(elem_img.attrib['alt'])
    <A> HREF=/3player/show/392/58784/1/Tallafornia
    <IMG> ALT="3player | Tallafornia, 01/02/2013. A fresh romance blossoms in the Tallafornia house. Marc challenges Cormac to a 'bench off' in the gym"
    <A> HREF=/3player/show/46/58765/1/Coronation-Street
    <IMG> ALT="3player | Coronation Street, 01/02/2013. Tyrone bumps into Kirsty in the street and tries to take Ruby from her pram"
    ../..
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webpage that displays a very large list of data. Since this
I current have the following XML fragment that I'm parsing to read its values
I currently have a webpage that displays an application. The purpose of this was
I am currently updating a webpage that has some very simple data displayed in
I currently developing a webpage using C#. I have a DropdownList data bound with
Suppose you have a webpage that is displaying data in columns. I'm currently separating
I currently have a webpage where an iframe contains data that is stored into
Database resources, that can be accessed from webpage that I'm currently working on, have
I currently have a small slideshow on my webpage that is being powered by
I really have read the other articles that cover this subject. But I seem

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.