473,809 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get document as normal text and not as binary data

Hi.

I used urllib2 to load a html-document through http. But my problem
is:
The loaded contents are returned as binary data, that means that every
character is displayed like lĂ€Ăt, for example. How can I get the
contents as normal text?

My script was:

import urllib2
req = urllib2.Request (url)
f = urllib2.urlopen (req)
contents = f.read()
print contents
f.close()

Thanks!

Markus
Jul 18 '05 #1
8 2219
Markus Franz wrote:
Hi.

I used urllib2 to load a html-document through http. But my problem
is:
The loaded contents are returned as binary data, that means that every
character is displayed like lĂ€Ăt, for example. How can I get the
contents as normal text?


You get what the server sends. That is always binary - either it _is_ a
binary file, or maybe in an unknown encoding.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Markus Franz wrote:
I used urllib2 to load a html-document through http. But my problem
is: The loaded contents are returned as binary data, that means that every
character is displayed like lĂ?Ăt, for example. How can I get the
contents as normal text?

My script was:

import urllib2
req = urllib2.Request (url)
f = urllib2.urlopen (req)
adding

print f.headers

and checking the header fields (especially the content-type) may help you
figure out what's going on...
contents = f.read()
print contents
f.close()


</F>

Jul 18 '05 #3
Diez B. Roggisch wrote:
You get what the server sends. That is always binary - either it _is_ a
binary file, or maybe in an unknown encoding.


And how can I convert those binary data to a "normal" string with
"normal" characters?

Best regards

Markus
Jul 18 '05 #4
Markus Franz wrote:
Diez B. Roggisch wrote:
You get what the server sends. That is always binary - either it _is_ a
binary file, or maybe in an unknown encoding.


And how can I convert those binary data to a "normal" string with
"normal" characters?


There is no "normal" - it's just bytes, and a string is just bytes. No
difference, no translation necessary.

As others have said: look into the http header what the server is trying to
transmit - maybe an image. The mimetype header is telling you that.

Or use wget to fetch the url and look what you get - it shouldn't look
different.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #5
Addendum: If you give us the url you're fetching data from, we might be able
to look at the delivered data ourselves.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #6
Markus Franz wrote:
Hi.

I used urllib2 to load a html-document through http. But my problem
is:
The loaded contents are returned as binary data, that means that every
character is displayed like lĂ€Ăt, for example. How can I get the
contents as normal text?
My guess is the html is utf-8 encoded - your sample looks like utf-8-interpreted-as-latin-1. Try
contents = f.read().decode ('utf-8')

Kent

My script was:

import urllib2
req = urllib2.Request (url)
f = urllib2.urlopen (req)
contents = f.read()
print contents
f.close()

Thanks!

Markus

Jul 18 '05 #7
Kent Johnson wrote:
My guess is the html is utf-8 encoded - your sample looks like
utf-8-interpreted-as-latin-1. Try
contents = f.read().decode ('utf-8')


YES! That helped!

I used the following:

....
contents = f.read().decode ('utf-8')
contents = contents.encode ('iso-8859-15')
....

That was the perfect solution for my problem! Thanks a lot!

Best regards

Markus
Jul 18 '05 #8
Diez B. Roggisch wrote:
Addendum: If you give us the url you're fetching data from, we might be able
to look at the delivered data ourselves.


To guess my problem please have a look at the document title of
<http://portal.suse.de/sdb/de/1997/01/xntp.html>

Markus
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2793
by: Rune Froysa | last post by:
Trying something like:: import xmlrpclib svr = xmlrpclib.Server("http://127.0.0.1:8000") svr.test("\x1btest") Failes on the server with:: xml.parsers.expat.ExpatError: not well-formed (invalid token) (Smaller test-case: xmlrpclib.loads(xmlrpclib.dumps(('\x1btest',))))
5
6563
by: Michael G. Schneider | last post by:
I know that using Word Automation inside an ASP page is no good idea. Anything I want to do in the current project is: open document, change some text, save and close document. Basically changing some variables, consting of a name embraced by special chars, to some value. As for example: change "" to "Michael". Does anybody know whether there is a way for achieving this with basic "file input / output". Can I regard a Word document as...
27
5049
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there any solid reasons to prefer text files over binary files files?
4
2047
by: Dante | last post by:
Hello. I have a Javascript that gets data from an XML document and displays it through javascript. The problem is that when I do dcfile.getElementsByTagName("subhead").firstChild.nodeName all I get is #text. Can you tell me why? Here is my code: /* The Dante-Cubed Standardized Markup Language Javascript API ------ Written by Sean M. Hall, Dante, and Peter-Paul Koch
8
9174
by: Asma | last post by:
Dear Sir, I am trying to find a way to open a Word document using C language and read the text of word doc into a variable. (Turbo C on Dos 6.0). Can anyone please tell me which libraries in C can be used to perform this task. Thanks you so much
12
1955
by: Rob Nicholson | last post by:
We've implemented functionality to allow a user to download a document (any document type) from the IIS server using the following code: Response.Clear() Response.ContentType = "application/x-msdownload" Response.AddHeader("Content-Disposition", "attachment; filename=" & System.IO.Path.GetFileName(FilePath)) Response.AddHeader("Content-Length", File.Length.ToString()) Response.WriteFile(FilePath) Response.End()
10
2085
by: Antoine De Groote | last post by:
Hi there, I have a word document containing pictures and text. This documents holds several 'ABCDEF' strings which serve as a placeholder for names. Now I want to replace these occurences with names in a list (members). I open both input and output file in binary mode and do the transformation. However, I can't open the resulting file, Word just telling that there was an error. Does anybody what I am doing wrong? Oh, and is this...
1
2835
by: Taras_96 | last post by:
Hi everyone, I'm using PHP and a DB to control access to files that have been uploaded by users. I am using the following PHP code snippet to deliver the file after the access rights have been checked. $theFile = file_get_contents(UPLOAD_PATH.$storedName); if($mimeExtension == '') { header("Content-Type: application/octet-stream\n");
0
7808
by: PracticalApps | last post by:
I looked to find a canned solution to create a Word document in my application and just couldn't find anything that just gets to the point. I would think, and I may be making too strong of an assumption here, that anyone looking for this kind of solution would just want a simple example that they can work from and expand upon. In my case, I just want to output a new Word document. I found that creating a simple wrapper class would make this...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.