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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:28:07+00:00 2026-06-04T00:28:07+00:00

I’m trying to parse a website’s source to extract text inside using lxml library.

  • 0

I’m trying to parse a website’s source to extract text inside using lxml library. Here is my code

import urllib2
from StringIO import StringIO
from lxml import html
from lxml.html.clean import Cleaner

cleaner = Cleaner(page_structure = False)
htmlsource = cleaner.clean_html(urllib2.urlopen("http://www.verycd.com/").read())
htmltree = html.parse(StringIO(htmlsource.decode("utf-8"))).getroot()
listnode = htmltree.xpath("*")
for node in listnode:
  print node.text_content().strip().encode("utf-8")

When I run the code in interactive console, the result looks like this (dev environment)

VeryCD电驴大全 - 分享互联网
用户名:
        密码:记住我 


        免费注册
         |
        忘记密码?



            首页 |
            商城 |
            专题 |
            乐园 |

            社区 |
            电驴 |
            网页游戏 |
            网址大全

But in production environment, all unicode characters displayed incorrectly

VeryCDçµé©´å¤§å¨ - å享äºèç½
ç¨æ·åï¼
        å¯ç ï¼è®°ä½æÂ 

        Â 
        å费注å
         |
        å¿è®°å¯ç ï¼



            é¦é¡µ |
            åå |
            ä¸é¢ |
            ä¹å |

            ç¤¾åº |
            çµé©´ |
            ç½é¡µæ¸¸æ |
            ç½å大å¨

Any idea how can I fix this?

EDIT

Seems like I found the problem here. I think there is some thing wrong with lxml builtin GAE. If I don’t use cleaner before parsing html, the output is fine.

# cleaner = Cleaner(page_structure = False)
# htmlsource = cleaner.clean_html(urllib2.urlopen("http://www.verycd.com/").read())
htmlsource = urllib2.urlopen("http://www.verycd.com/").read()
htmltree = html.parse(StringIO(htmlsource.decode("utf-8"))).getroot()
  • 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-04T00:28:08+00:00Added an answer on June 4, 2026 at 12:28 am

    Update: This bug is fixed in App Engine, so the following work-around should no longer be necessary.

    I’ve accepted this as a bug in either lxml or App Engine. But you can work around it using lxml.etree.parse and lxml.etree.HTMLParser (note that lxml.html is a simple wrapper around these two):

    import urllib2
    from StringIO import StringIO
    from lxml import etree
    from lxml.html.clean import Cleaner
    
    cleaner = Cleaner(page_structure = False)
    htmlsource = cleaner.clean_html(urllib2.urlopen("http://www.verycd.com/").read())
    htmlparser = etree.HTMLParser(encoding='utf-8')
    htmltree = etree.parse(StringIO(htmlsource.decode("utf-8")),
                           parser=htmlparser).getroot()
    listnode = htmltree.xpath("*")
    for node in listnode:
      print node.text.strip().encode("utf-8")
    

    That is:

    • Create an HTMLParser object, explicitly setting encoding='utf-8'.
    • Use etree.parse instead of html.parse; pass parser=htmlparser to etree.parse.
    • Use node.text instead of node.text_content().

    This works around the bug by explicitly telling the HTMLParser to use UTF-8 encoding instead of having it guess (it guesses Latin-1 incorrectly).

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

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.