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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:41:20+00:00 2026-05-26T08:41:20+00:00

I have a lxml etree HTMLParser object that I’m trying to build xpaths with

  • 0

I have a lxml etree HTMLParser object that I’m trying to build xpaths with to assert xpaths, attributes of the xpath and text of that tag. I ran into a problem when the text of the tag has either single-quotes(‘) or double-quotes(“) and I’ve exhausted all my options.

Here’s a sample object I created

parser = etree.HTMLParser()
tree = etree.parse(StringIO(<html><body><p align="center">Here is my 'test' "string"</p></body></html>), parser)

Here is the snippet of code and then different variations of the variable being read in

   def getXpath(self)
     xpath += 'starts-with(., \'' + self.text + '\') and '
     xpath += ('count(@*)=' + str(attrsCount) if self.exactMatch else "1=1") + ']'

self.text is basically the expected text of the tag, in this case: Here is my ‘test’ “string”

this fails when i try to use the xpath method of the HTMLParser object

tree.xpath(self.getXpath())

Reason is because the xpath that it gets is this ‘/html/body/p[starts-with(.,’Here is my ‘test’ “string”‘) and 1=1]’

How can I properly escape the single and double quotes from the self.text variable? I’ve tried triple quoting, wrapping self.text in repr(), or doing a re.sub or string.replace escaping ‘ and ” with \’ and \”

  • 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-26T08:41:21+00:00Added an answer on May 26, 2026 at 8:41 am

    According to what we can see in Wikipedia and w3 school, you should not have ' and " in nodes content, even if only < and & are said to be stricly illegal. They should be replaced by corresponding “predefined entity references”, that are &apos; and &quot;.

    By the way, the Python parsers I use will take care of this transparently: when writing, they are replaced; when reading, they are converted.

    After a second reading of your answer, I tested some stuff with the ' and so on in Python interpreter. And it will escape everything for you!

    >>> 'text {0}'.format('blabla "some" bla')
    'text blabla "some" bla'
    >>> 'ntsnts {0}'.format("ontsi'tns")
    "ntsnts ontsi'tns"
    >>> 'ntsnts {0}'.format("ontsi'tn' \"ntsis")
    'ntsnts ontsi\'tn\' "ntsis'
    

    So we can see that Python escapes things correctly. Could you then copy-paste the error message you get (if any)?

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

Sidebar

Related Questions

I'm trying to inject an attribute into lxml.etree._Element , but as that module is
I have an xml file that I'm using etree from lxml to work with,
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
I'm trying to test some python code that uses urllib2 and lxml. I've seen
I have been struggling to be able to do from lxml import etree (
I have this script- import lxml from lxml.cssselect import CSSSelector from lxml.etree import fromstring
I have this xpath pattern: tags = doc.xpath('/html/body//a[text() = ' + name.encode('utf8') + ']/@href'
I am trying to migrate some code from using ElementTree to using lxml.etree and
I am trying to build lxml for Python 2.7 on Windows 64 bit machine.
I want to scrape some html pages that have nested form elements with lxml.

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.