473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Instead of saving text files i need as html

I have the following code which takes a list of urls
"http://google.com", without the quotes ofcourse, and then saves there
source code as a text file. I wan to alter the code so that for the
list of URLs an html file is saved.

-----begin-----
import urllib
urlfile = open(r'c:\temp\url.txt', 'r')
for lines in urlfile:
try:
outfilename = lines.replace('/', '-')
urllib.urlretrieve(lines.strip('/n'), 'c:\\temp\\' \
+ outfilename.strip('\n')[7:] + '.txt')
except:
pass
-----end-----

Jun 8 '06 #1
7 1474
Then just write HTML around your list. I would guess
you want them inside a table. Just write appropriate
HTML tags before/after the urls. If you want the URLs
to be clickable make them in into <a href>url</a> lines.

-Larry Bates

Shani wrote:
I have the following code which takes a list of urls
"http://google.com", without the quotes ofcourse, and then saves there
source code as a text file. I wan to alter the code so that for the
list of URLs an html file is saved.

-----begin-----
import urllib
urlfile = open(r'c:\temp\url.txt', 'r')
for lines in urlfile:
try:
outfilename = lines.replace('/', '-')
urllib.urlretrieve(lines.strip('/n'), 'c:\\temp\\' \
+ outfilename.strip('\n')[7:] + '.txt')
except:
pass
-----end-----

Jun 8 '06 #2
"Shani" <Sh******@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
I have the following code which takes a list of urls
"http://google.com", without the quotes ofcourse, and then saves there
source code as a text file. I wan to alter the code so that for the
list of URLs an html file is saved.

-----begin-----
import urllib
urlfile = open(r'c:\temp\url.txt', 'r')
for lines in urlfile:
try:
outfilename = lines.replace('/', '-')
urllib.urlretrieve(lines.strip('/n'), 'c:\\temp\\' \
+ outfilename.strip('\n')[7:] + '.txt')
except:
pass
-----end-----


Is this what you mean?

-----begin-----
import urllib
urlfile = open(r'c:\temp\url.txt', 'r')
for lines in urlfile:
try:
outfilename = lines.replace('/', '-')
urllib.urlretrieve(lines.strip('/n'), 'c:\\temp\\' \
+ outfilename.strip('\n')[7:] + '.html')
except:
pass
-----end-----
Louis
Jun 8 '06 #3
"Shani" <Sh******@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
I have the following code which takes a list of urls
"http://google.com", without the quotes ofcourse, and then saves there
source code as a text file. I wan to alter the code so that for the
list of URLs an html file is saved.

-----begin-----
import urllib
urlfile = open(r'c:\temp\url.txt', 'r')
for lines in urlfile:
try:
outfilename = lines.replace('/', '-')
urllib.urlretrieve(lines.strip('/n'), 'c:\\temp\\' \
+ outfilename.strip('\n')[7:] + '.txt')
except:
pass
-----end-----

Or is this what you mean?
-----begin-----
import urllib
urlfile = open('c:\\temp\\url.txt', 'r')
newurlfile = open('c:\\temp\\newurls.html', 'w')
newurlfile.write('<html> \n<body>\n')
for lines in urlfile:
try:
if lines == '\n':
pass
else:
lines = '<a href="' + lines.strip() +'">'\
+ lines.strip() + '</a>' + '<br>\n'
newurlfile.write(lines)
except:
pass
newurlfile.write('</body> \n</html>')
urlfile.close()
newurlfile.close()
-----end-----
Louis
Jun 8 '06 #4
"3c273" <no****@nospam.com> wrote in message
news:e6*********@enews2.newsguy.com...
Or is this what you mean?
-----begin-----
import urllib
urlfile = open('c:\\temp\\url.txt', 'r')
newurlfile = open('c:\\temp\\newurls.html', 'w')
newurlfile.write('<html> \n<body>\n')
for lines in urlfile:
try:
if lines == '\n':
pass
else:
lines = '<a href="' + lines.strip() +'">'\
+ lines.strip() + '</a>' + '<br>\n'
newurlfile.write(lines)
except:
pass
newurlfile.write('</body> \n</html>')
urlfile.close()
newurlfile.close()
-----end-----
Louis

Oops, I guess we don't need "import urllib" anymore.
Louis
Jun 8 '06 #5
Shani wrote:
I have the following code which takes a list of urls
"http://google.com", without the quotes ofcourse, and then saves there
source code as a text file. I wan to alter the code so that for the
list of URLs an html file is saved.


What you write in a text file is up to you - and AFAICT, HTML is still
a text format.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 8 '06 #6
> Is this what you mean?

-----begin-----
import urllib
urlfile = open(r'c:\temp\url.txt', 'r')
for lines in urlfile:
try:
outfilename = lines.replace('/', '-')
urllib.urlretrieve(lines.strip('/n'), 'c:\\temp\\' \
+ outfilename.strip('\n')[7:] + '.html')
except:
pass
-----end-----


[laughs] I suspect the urlretrieve line should contain
"strip('\n')" instead of "strip('/n')", but otherwise, the
original code looked pretty kosher. I'm not sure what the odd
slicing is for, but I'll presume the OP knows what they're doing.

While not a python solution, the standard *nix tool would be
either wget or curl:

bash> wget -i listofurls.txt

which is freely available with the Cygwin suite of GNU tools for
Win32 platforms.

-tkc

Jun 8 '06 #7
Tim Chase <ti*@thechases.com> wrote:
[ ... ]
urllib.urlretrieve(lines.strip('/n'), 'c:\\temp\\' \
+ outfilename.strip('\n')[7:] + '.html')

[ ... ] I'm not sure what the odd
slicing is for, but I'll presume the OP knows what they're doing.


It's taking the "http://" off the front of the URL.
len("http://")

7

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Jun 9 '06 #8

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

Similar topics

5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
3
by: tigrrgrr42 | last post by:
I am working(vb.net03and05) with word documents stored in a sql db and I am currently bringing them from a byte array into a temp file to pop into word and make word do its thing as a com object. ...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
1
by: fatjoez | last post by:
Hey there. I've been trying to modify my file upload script so that it handles 10 files instead of one. i was thinking the most straightforward way would be to add a FOR LOOP? placed...
10
by: JoeC | last post by:
I am writing a game and all my game pieces are stored in a single vector of piece handles. I have the basics I can read and write char and number files but I am trying to do comthing more...
7
by: Dave Kelly | last post by:
There has to be a name for what I want to do and I don't know what words to google for. I have a form here: http://www.texasflyfishers.org/firstpage.htm I want to submit it to the server and...
3
by: pozze | last post by:
Hi, I've just made the change from ASP to .net. I have a file (code below) that saves a user submitted file to a MS SQL 2005 database. It collects the file name, file size, file type, and lastly...
9
by: Yamasassy | last post by:
HI im having problems to saving to a .txt file, i need to be able to store a number of employees to this file (and beable to open and these files to load in to text boxes)and retrieve then for later...
6
by: Eddie | last post by:
Hi all, I am displaying a number of reports, and giving the users an option to display them on the web or download them to Excel. If they want the Excel file, I just use the PHP header command...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.