Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.csv files
#1
I want to write a web page to a csv file. Error said file not open, but it should be. here is the code:

import csv
import urllib2


with open("results.csv", "w") as f:
    writer = csv.writer(f)
    urllib2.urlopen("https://www.kali.org/")

    writer.writerow([urllib2.urlopen])
This is the first time I try this.
thank you Huh
Reply
#2
Do you have the file open already in another application?
Is it the file open or the urlopen that is showing an error?
Show the full error traceback when reporting an error.
Reply
#3
No, I don't, but it does put this in the file.....(<function urlopen at 0x0000000002C9C048>).... So it is working, but it is not putting the html of the site in the csv file

i try all the ways I know of doing this. I going out and read some more on this. Here is my latest code:

import csv
import urllib2


with open("results.csv", "w") as f:
    writer = csv.writer(f)
    url = urllib2.urlopen("https://www.kali.org/").read()
  
    writer.writerow([url])

Well it looks like I did not do something right I got the Hated Read this message.
Sorry for what I did.
Reply
#4
I never used csv module but what I see in the internet you have to open the file in 'wb' mode. Then 
html = urllib.urlopen('http://example.com/').read()

for row in html:
    csv_writer.writerow(row)
I have missed creating of the writer object but you don't have to :)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Well look here and see if I did it right, I get this error:

line 9
for row in html
TypeError: 'function' object is not iterable

import csv
import urllib2


with open("results.csv", "w") as f:
    writer = csv.writer(f)
    urllib2.urlopen("https://www.kali.org/")
    html = urllib2.urlopen
    for row in html:
      csv_writer.writerow(row)
What did I do wrong this time?
Reply
#6
html = urllib2.urlopen('http://example.com/')
As you did it you create an instance of urllib2.urlopen() called html.
>>> import urllib2
>>> get_html = urllib2.urlopen
>>> html = get_html('http://google.com/')
>>> page = html.read()
>>> print page[:100]
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="bg"><head><meta content
>>> 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
I am running 2.7.12, it will not run, see error:

>>> import urllib2
>>> get_html = urllib2.urlopen
>>> page = html.read()

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    page = html.read()
NameError: name 'html' is not defined
>>> 
thank you for your help
Reply
#8
The proper way to get the html code of a web page via urllib2 is:

import urllib2
page = urllib2.urlopen("http://google.com/")
html = page.read()
or 

import urllib2
html = urllib2.urlopen("http://google.com").read()
Or better use Requests
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(Nov-06-2016, 09:52 PM)Blue Dog Wrote: >>> get_html = urllib2.urlopen
>>> page = html.read()
you named it get_html but called html
Recommended Tutorials:
Reply
#10
Ok, got it working like I want, well all most, still have to do one more thing.

Here is the code if anyone want to use it, have fun:

import urllib2
get_html = urllib2.urlopen
html = get_html('http://google.com/')
page = html.read()
f = open("html.txt","w")
f.write(page)
f.close()
print page[:100]
I am dome for the day, turn this dam computer on at 4A.M. today
Good night one and all.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020