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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:13:24+00:00 2026-05-23T10:13:24+00:00

When I read some (but not all) HTML files in python using a urllib2

  • 0

When I read some (but not all) HTML files in python using a urllib2 opener, on some files I’m getting text filled with lots of backslashes and the unicode 003c strings. I’m sending this text into BeautifulSoup and am having trouble finding what I’m looking for with findAll(), and I’m now thinking it’s due to all these unicode strings.

What’s going on with this, and how do I get rid of it?

Approaches like soup.prettify() have no effect.

Here’s some example code (this comes from a Facebook profile)

\\u003cdiv class=\\"pas status fcg\\">Loading...\\u003c\\/div>
\\u003c\\/div>\\u003cdiv class=\\"uiTypeaheadView fbChatBuddyListTypeaheadView dark hidden_elem\\" id=\\"u971289_14\\">\\u003c\\/div>
\\u003c\\/div>\\u003c\\/div>\\u003cdiv class=\\"fbNubFlyoutFooter\\">
\\u003cdiv class=\\"uiTypeahead uiClearableTypeahead fbChatTypeahead\\" id=\\"u971289_15\\">
\\u003cdiv class=\\"wrap\\">\\u003clabel class=\\"clear uiCloseButton\\" for=\\"u971291_21\\">

This same HTML page looks fine and normal in a ‘view source’ window.

EDIT: Here’s the code that’s producing that text. What’s strange is that I don’t get this kind of output from other HTML pages. Note that I’ve replaced the username and password with USERNAME and PASSWORD for here. You could try this on your own FB profile if you replace those two.

fbusername = "USERNAME@gmail.com"
fbpassword = "PASSWORD"
cookiefile = "facebook.cookies"

cj = cookielib.MozillaCookieJar(cookiefile)
if os.access(cookiefile, os.F_OK):
    cf.load()

opener = urllib2.build_opener(
    urllib2.HTTPRedirectHandler(),
    urllib2.HTTPHandler(debuglevel=0),
    urllib2.HTTPSHandler(debuglevel=0),
    urllib2.HTTPCookieProcessor(cj)
)

opener.addheaders = [('User-agent','Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1'),('Referer','http://www.facebook.com/')]

def facebooklogin():
    logindata = urllib.urlencode({
        'email' : fbusername,
        'pass' : fbpassword,
    })

    response = opener.open("https://login.facebook.com/login.php",logindata)
    return ''.join(response.readlines())


print "Logging in to Facebook...\n"
facebooklogin()
facebooklogin()
print "Successful.\n"

fetchURL = 'http://www.facebook.com/USERNAME?ref=profile&v=info'

f = opener.open(fetchURL)
fba = f.read()
f.close()
soup = BeautifulSoup(fba)
print soup
  • 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-23T10:13:24+00:00Added an answer on May 23, 2026 at 10:13 am

    The u""" construct is for Python 2. You omit the u for Python 3.

    >>> a=u"""\\u003cdiv class=\\"pas status fcg\\">Loading...\\u003c\\/div>
    ... \\u003c\\/div>\\u003cdiv class=\\"uiTypeaheadView fbChatBuddyListTypeaheadView dark hidden_elem\\" id=\\"u971289_14\\">\\u003c\\/div>
    ... \\u003c\\/div>\\u003c\\/div>\\u003cdiv class=\\"fbNubFlyoutFooter\\">
    ... \\u003cdiv class=\\"uiTypeahead uiClearableTypeahead fbChatTypeahead\\" id=\\"u971289_15\\">
    ... \\u003cdiv class=\\"wrap\\">\\u003clabel class=\\"clear uiCloseButton\\" for=\\"u971291_21\\">
    ... """
    >>> print(a.decode('unicode_escape')).replace('\\/', '/')
    <div class="pas status fcg">Loading...<\/div>
    <\/div><div class="uiTypeaheadView fbChatBuddyListTypeaheadView dark hidden_elem" id="u971289_14"><\/div>
    <\/div><\/div><div class="fbNubFlyoutFooter">
    <div class="uiTypeahead uiClearableTypeahead fbChatTypeahead" id="u971289_15">
    <div class="wrap"><label class="clear uiCloseButton" for="u971291_21">
    

    I hope this helps. If not, please improve the information you give in your question.

    EDIT: suggested answer now changes \/ to / too.

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

Sidebar

Related Questions

I read the SDK document, but can not understand some details, need some help.
For some reasons, SSIS is not avalialble. I read about OPENROWSET, but I will
I read some source code of ZenTest but didn't find where it is implemented.
I've read through some of the post on here about closures and currying but
I've read some about .egg files and I've noticed them in my lib directory
I need to read some large files (from 50k to 100k lines), structured in
I read some of the answers on here re: testing views and controllers, and
I read some properties from an xml file, amongst which is a string that
So far I've read some blog articles about cloud computing and services for hosting
I did some timing tests and also read some articles like this one (last

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.