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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:36:55+00:00 2026-05-18T01:36:55+00:00

I have some html file with span elements: <html> <body> <span class=one>Text</span>some text</br> <span

  • 0

I have some html file with span elements:

<html>
<body>
<span class="one">Text</span>some text</br>
<span class="two">Привет</span>Текст на русском</br>
</body>
</html>

To get “some text” :

# -*- coding:cp1251 -*-
import lxml
from lxml import html

filename = "t.html"
fread = open(filename, 'r')
source = fread.read()

tree = html.fromstring(source)
fread.close()


tags = tree.xpath('//span[@class="one" and text()="Text"]') #This OK
print "name: ",tags[0].text
print "value: ",tags[0].tail

tags = tree.xpath('//span[@class="two" and text()="Привет"]') #This False

print "name: ",tags[0].text
print "value: ",tags[0].tail

This show:

name: Text
value: some text

Traceback: ... in line `tags = tree.xpath('//span[@class="two" and text()="Привет"]')`
    ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes

How to solve this problem?

  • 1 1 Answer
  • 3 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-18T01:36:55+00:00Added an answer on May 18, 2026 at 1:36 am

    lxml

    (As observed, this is a bit dodgy between system encodings and apparently doesn’t work properly in Windows XP, though it did in Linux.)

    I got it to work by decoding the source string – tree = html.fromstring(source.decode('utf-8')):

    # -*- coding:cp1251 -*-
    import lxml
    from lxml import html
    
    filename = "t.html"
    fread = open(filename, 'r')
    source = fread.read()
    
    tree = html.fromstring(source.decode('utf-8'))
    fread.close()
    
    
    tags = tree.xpath('//span[@class="one" and text()="Text"]') #This OK
    print "name: ",tags[0].text
    print "value: ",tags[0].tail
    
    tags = tree.xpath('//span[@class="two" and text()="Привет"]') #This is now OK too
    
    print "name: ",tags[0].text
    print "value: ",tags[0].tail
    

    This means that the actual tree is all unicode objects. If you just put the xpath parameter as a unicode it finds 0 matches.

    BeautifulSoup

    I prefer to use BeautifulSoup for any of this sort of stuff, anyway. Here is my interactive session; I saved the file in cp1251.

    >>> from BeautifulSoup import BeautifulSoup
    >>> filename = '/tmp/cyrillic'
    >>> fread = open(filename, 'r')
    >>> source = fread.read()
    >>> source  # Scary
    '<html>\n<body>\n<span class="one">Text</span>some text</br>\n<span class="two">\xcf\xf0\xe8\xe2\xe5\xf2</span>\xd2\xe5\xea\xf1\xf2 \xed\xe0 \xf0\xf3\xf1\xf1\xea\xee\xec</br>\n</body>\n</html>\n'
    >>> source = source.decode('cp1251')  # Let's try getting this right.
    u'<html>\n<body>\n<span class="one">Text</span>some text</br>\n<span class="two">\u041f\u0440\u0438\u0432\u0435\u0442</span>\u0422\u0435\u043a\u0441\u0442 \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c</br>\n</body>\n</html>\n'
    >>> soup = BeautifulSoup(source)
    >>> soup  # OK, that's looking right now. Note the </br> was dropped as that's bad HTML with no meaning.
    <html>
    <body>
    <span class="one">Text</span>some text
    <span class="two">Привет</span>Текст на русском
    </body>
    </html>
    
    >>> soup.find('span', 'one').findNextSibling(text=True)
    u'some text'
    >>> soup.find('span', 'two').findNextSibling(text=True)  # This looks a bit daunting ...
    u'\u0422\u0435\u043a\u0441\u0442 \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c'
    >>> print _  # ... but it's not, really. Just Unicode chars.
    Текст на русском
    >>> # Then you may also wish to get things by text:
    >>> print soup.find(text=u'Привет').findParent().findNextSibling(text=True)
    Текст на русском
    >>> # You can't get things by attributes and the contained NavigableString at the same time, though. That may be a limitation.
    

    At the end of that, it’s possibly worth while considering trying source.decode('cp1251') instead of source.decode('utf-8') when you’re taking it from the filesystem. lxml may actually work then.

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

Sidebar

Related Questions

I have some html file: <html> <body> <span class=text>One</span>some text1</br> <span class=cyrillic>Мир</span>some text2</br> </body>
I have this text file filled with some html code; <span>test</span><p>welcome</p> <span>Guest</span><p><img class=insertedImage src=/dynamictemplate/uploads/temp/167-5-IMG_0755.JPG
I have this html file where I want to overlay some text over another.
I have an XSLT file generating plain HTML. I need to wrap some elements
I have some html like the following: <div class=control-group> <input type=text data-bind=value: $data.DealCode name=DealCode
I have a .html file with some scripts in it. Works fine on localhost
I have the following file structure: |- index.html vendor |- jquery.min.js (some libraries) js
I have an IntentService that is parsing some xlm file to create a html
I have one HTML file and what I have to do is to store
I have a HTML file, which generates a web page. Users can enter some

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.