Hello, me again :)
With this code:
>>> from BeautifulSoup import BeautifulSoup >>> import urllib2 >>> url = urllib2.urlopen('http://www.python.org').read() >>> soup = BeautifulSoup(url) >>> links = soup('a') >>> print links
A list of links printed into the terminal. I want to send the list into a text file, i tried this:
>>> with open('python-links.txt.', 'w') as f: ... f.write(links)
But there was an error:
File "<stdin>", line 2, in <module> TypeError: expected a character buffer object
And one more question; as that list looks like this: (I will copy only small part of the list)
[<a href="#content" title="Skip to content">Skip to content</a>, <a id="close-python-network" class="jump-link" href="#python-network" aria-hidden="true"> <span aria-hidden="true" class="icon-arrow-down"><span>▼</span></span> Close </a>, <a href="/" title="The Python Programming Language" class="current_item selectedcurrent_branch selected">Python</a>, <a href="/psf-landing/" title="The Python Software Foundation">PSF</a>,
So how can i drop each link into a new line?
I tried this:
>>> text = '\n'.join(links)
But i got this error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sequence item 0: expected string, Tag found
How can i do that?