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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:00:17+00:00 2026-06-07T16:00:17+00:00

I am using BeautifulSoup to scrape an URL and I had the following code,

  • 0

I am using BeautifulSoup to scrape an URL and I had the following code, to find the td tag whose class is 'empformbody':

import urllib
import urllib2
from BeautifulSoup import BeautifulSoup

url =  "http://www.example.com/servlet/av/ResultTemplate=AVResult.html"
req = urllib2.Request(url)
response = urllib2.urlopen(req)
the_page = response.read()
soup = BeautifulSoup(the_page)

soup.findAll('td',attrs={'class':'empformbody'})

Now in the above code we can use findAll to get tags and information related to them, but I want to use XPath. Is it possible to use XPath with BeautifulSoup? If possible, please provide me example code.

  • 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-07T16:00:19+00:00Added an answer on June 7, 2026 at 4:00 pm

    Nope, BeautifulSoup, by itself, does not support XPath expressions.

    An alternative library, lxml, does support XPath 1.0. It has a BeautifulSoup compatible mode where it’ll try and parse broken HTML the way Soup does. However, the default lxml HTML parser does just as good a job of parsing broken HTML, and I believe is faster.

    Once you’ve parsed your document into an lxml tree, you can use the .xpath() method to search for elements.

    try:
        # Python 2
        from urllib2 import urlopen
    except ImportError:
        from urllib.request import urlopen
    from lxml import etree
    
    url =  "http://www.example.com/servlet/av/ResultTemplate=AVResult.html"
    response = urlopen(url)
    htmlparser = etree.HTMLParser()
    tree = etree.parse(response, htmlparser)
    tree.xpath(xpathselector)
    

    There is also a dedicated lxml.html() module with additional functionality.

    Note that in the above example I passed the response object directly to lxml, as having the parser read directly from the stream is more efficient than reading the response into a large string first. To do the same with the requests library, you want to set stream=True and pass in the response.raw object after enabling transparent transport decompression:

    import lxml.html
    import requests
    
    url =  "http://www.example.com/servlet/av/ResultTemplate=AVResult.html"
    response = requests.get(url, stream=True)
    response.raw.decode_content = True
    tree = lxml.html.parse(response.raw)
    

    Of possible interest to you is the CSS Selector support; the CSSSelector class translates CSS statements into XPath expressions, making your search for td.empformbody that much easier:

    from lxml.cssselect import CSSSelector
    
    td_empformbody = CSSSelector('td.empformbody')
    for elem in td_empformbody(tree):
        # Do something with these table cells.
    

    Coming full circle: BeautifulSoup itself does have very complete CSS selector support:

    for cell in soup.select('table#foobar td.empformbody'):
        # Do something with these table cells.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code. import urllib2 import urllib import json from BeautifulSoup import BeautifulSoup
I have something like this using BeautifulSoup: for line in lines: code = l.find('span',
Scraping pages using BeautifulSoup; trying to filter out links that end in ...html#comments Code
Here is the my python code using BeautifulSoup. The main issue is with the
I'm trying to Parse the following HTML pages using BeautifulSoup (I'm going to parse
I have this code that fetches some text from a page using BeautifulSoup soup=
I am using BeautifulSoup to scrape data from a webpage. I want to compare
I am currrently using BeautifulSoup to scrape some websites, however I have a problem
I'm using BeautifulSoup . I have to find any reference to the <div> tags
I'm using beautifulSoup to scrape a page that has a ISO-8859-1 encoding however I've

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.