Quote:
|
Originally Posted by zem52887
and how can I set the printed links to one value? (or is this even necessary) or are they already set to "a"
|
The simplest way is to add each value to a string. A string is programming terminology for a piece of text.
The first thing to do is to create a blank string:
Next, instead of printing out, we add to this string, instead:
for link in links:
title = link.string.replace("\n", " ")
all_links += title + " - " + link['href'] + "\n" We can also use list comprehensions for this. Lets say we want a list of urls. We could use a list comprehension like so:
urls = [link['href'] for link in links]
Though for practical purposes, there's no need to split up the links any further. There's little difference between this:
# print out all urls
urls = [link['href'] for link in links]
for url in urls:
print url
And this:
# print out all urls
for link in links:
print link['href']
If you've been looking through the Python tutorial, you'll know that any line with a # in front is ignored by Python and is for human benefit only. This is called a "comment".