Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98984
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-11-18 09:15 -0800 |
| References | <9365cf2f-e9c7-4338-83b4-ce3d1d7ce1d6@googlegroups.com> <mailman.418.1447865881.16136.python-list@python.org> <099133ed-c6df-4f5c-b47b-f1cf464511f6@googlegroups.com> |
| Message-ID | <9ddeb643-292f-4d5a-a891-83bee1d35c2f@googlegroups.com> (permalink) |
| Subject | Re: How can I export data from a website and write the contents to a text file? |
| From | ryguy7272 <ryanshuell@gmail.com> |
On Wednesday, November 18, 2015 at 12:04:16 PM UTC-5, ryguy7272 wrote:
> On Wednesday, November 18, 2015 at 11:58:17 AM UTC-5, Chris Angelico wrote:
> > On Thu, Nov 19, 2015 at 3:37 AM, ryguy7272 <> wrote:
> > > text_file = open("C:/Users/rshuell001/Desktop/excel/Text1.txt", "wb")
> > > z = str(link)
> > > text_file.write(z + "\n")
> > > text_file.write("\n")
> > > text_file.close()
> >
> > You're opening the file every time you go through the loop,
> > overwriting each time. Instead, open the file once, then start the
> > loop, and then close it at the end. You can use a 'with' statement to
> > do the closing for you, or you can do it the way you are here.
> >
> > ChrisA
>
>
>
> Thanks. What would the code look like? I tried the code below, and got the same results.
>
>
> for item in soup.find_all(class_='lister-list'):
> for link in item.find_all('a'):
> #print(link)
> z = str(link)
> text_file = open("C:/Users/rshuell001/Desktop/excel/Text1.txt", "wb")
> text_file.write(z + "\n")
> text_file.close()
Oh, I see, it's like this:
text_file = open("C:/Users/rshuell001/Desktop/excel/Text1.txt", "wb")
var_file.close()
soup = BeautifulSoup(var_html)
for item in soup.find_all(class_='lister-list'):
for link in item.find_all('a'):
#print(link)
z = str(link)
text_file.write(z + "\n")
text_file.close()
However, it's not organized very well, and it's hard to read. I thought the '\n' would create a new line after one line was written. Now, it seems like everything is jumbled together. Kind of weird. Am I missing something?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 08:37 -0800
Re: How can I export data from a website and write the contents to a text file? Chris Angelico <rosuav@gmail.com> - 2015-11-19 03:57 +1100
Re: How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 09:03 -0800
Re: How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 09:15 -0800
Re: How can I export data from a website and write the contents to a text file? Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-18 17:19 +0000
Re: How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 09:40 -0800
Re: How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 09:43 -0800
Re: How can I export data from a website and write the contents to a text file? Patrick Hess <patrickhess@gmx.net> - 2015-11-19 20:17 +0100
Re: How can I export data from a website and write the contents to a text file? Michael Torrie <torriem@gmail.com> - 2015-11-20 10:44 -0700
Re: How can I export data from a website and write the contents to a text file? Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-11-18 18:05 +0000
Re: How can I export data from a website and write the contents to a text file? Random832 <random832@fastmail.com> - 2015-11-18 16:38 -0500
csiph-web