0

What if I needed more than one hyperlink. Just one after another, each on a new line. I'm getting "positional argument follows keyword argument".

out = open("index.html", "w")
out.write(page.format(
heading = "<h1>Road Fatalities in Australia</h1>", 
contents1 = "<p>On this site you will find road fatality statistical data.</p>", 
contents2 = "<p>...</p>", 
contents3 = "<p>...</p>", 
link = '<a href="fatalities_per_year.html">A Look at Death Rates Over the Years</a>',
'<a href="fatalities_per_state.html">Which State has the Highest Fatality Rate?</a>',
'<a href="fatalities_per_day.html">Which Day of the Week is Deadliest?</a>',
'<a href="fatalities_per_time.html">Which Time of Day is the Deadliest?</a>',
'<a href="fatalities_trucks.html">How Many Trucks are in Fatal Crashes</a>',
'<a href="fatalities_speed_zones.html">Which Speed Zones are the Deadliest?</a>',
'<a href="fatalities_gender.html">Does Your Gender Increase the Risk of Dying?</a>',
'<a href="fatalities_age.html">Does Your Age Have Anything to do With Death-rates?</a>',
'<a href="fatalities_user.html">Fatality Data</a>'))
out.close()

I have also tried this:

out = open("index.html", "w")
out.write(page.format(
heading = "<h1>Road Fatalities in Australia</h1>", 
contents1 = "<p>On this site you will find road fatality statistical data.</p>", 
contents2 = "<p>...</p>", 
contents3 = "<p>...</p>", 
link = '<ul>
    <li><a href="fatalities_per_year.html">A Look at Death Rates Over the Years</a></li>
    <li><a href="fatalities_per_state.html">Which State has the Highest Fatality Rate?</a></li>
    <li><a href="fatalities_per_day.html">Which Day of the Week is Deadliest?</a></li>
    <li><a href="fatalities_per_time.html">Which Time of Day is the Deadliest?</a></li>
    <li><a href="fatalities_trucks.html">How Many Trucks are in Fatal Crashes</a></li>
    <li><a href="fatalities_speed_zones.html">Which Speed Zones are the Deadliest?</a></li>
    <li><a href="fatalities_gender.html">Does Your Gender Increase the Risk of Dying?</a></li>
    <li><a href="fatalities_age.html">Does Your Age Have Anything to do With Death-rates?</a></li>
    <li><a href="fatalities_user.html">Fatality Data</a></li>
</ul>'
out.close()

I just need the links in a list, one after another. Nothing special, like follows:

A look at Death Rates Over the Years

Which State has the Highest Fatality Rate?

...etc...

Thanks in advance

2
  • What's the error message you get in your second example? Commented May 8, 2018 at 12:30
  • just to clarify. You got an issue with our html formatting? Or do you want to extract the links into an python array? Commented May 8, 2018 at 12:33

3 Answers 3

1

use BeautifulSoup

from bs4 import BeautifulSoup

link = '''<ul>
<li><a href="fatalities_per_year.html">A Look at Death Rates Over the Years</a></li>
<li><a href="fatalities_per_state.html">Which State has the Highest Fatality Rate?</a></li>
<li><a href="fatalities_per_day.html">Which Day of the Week is Deadliest?</a></li>
<li><a href="fatalities_per_time.html">Which Time of Day is the Deadliest?</a></li>
<li><a href="fatalities_trucks.html">How Many Trucks are in Fatal Crashes</a></li>
<li><a href="fatalities_speed_zones.html">Which Speed Zones are the Deadliest?</a></li>
<li><a href="fatalities_gender.html">Does Your Gender Increase the Risk of Dying?</a></li>
<li><a href="fatalities_age.html">Does Your Age Have Anything to do With Death-rates?</a></li>
<li><a href="fatalities_user.html">Fatality Data</a></li>
</ul>'''

soup = BeautifulSoup(link,"lxml")
tags = soup.select("ul li a")
items = [tag.attrs['href'] for tag in tags]
print(items)

ouput

['fatalities_per_year.html', 'fatalities_per_state.html', 'fatalities_per_day.html', 'fatalities_per_time.html', 'fatalities_trucks.html', 'fatalities_speed_zones.html', 'fatalities_gender.html', 'fatalities_age.html', 'fatalities_user.html']
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't need to use the beautifulsoup, just needed to format the list properly like you have there. The list comes up as a bulleted list but that's fine for this purpose. Cheers
0

The error positional argument follows keyword argument that you're getting is due the way you're passing the arguments to the page.format() function.

In your first example link argument is a bunch of strings separated by the comma. But only the first line:

link = '<a href="fatalities_per_year.html">A Look at Death Rates Over the Years</a>'

is recognized as keyword argument named link, rest of the lines are treated like positional arguments without keyword assigned to them. This is a violation in Python, positional (not named arguments) can only be used before keyword (named) arguments, thus an error.

In your second example you are trying to pass multiline string as link argument but you use single quotes to do this. In order to use multiline string in Python you must enclose it in triple quotes

some_string = """line 1
line 2"""

The easiest fix for you would be to do:

out = open("index.html", "w")

out.write(page.format(
heading = "<h1>Road Fatalities in Australia</h1>", 
contents1 = "<p>On this site you will find road fatality statistical data.</p>", 
contents2 = "<p>...</p>", 
contents3 = "<p>...</p>", 
link = '''<ul>
    <li><a href="fatalities_per_year.html">A Look at Death Rates Over the Years</a></li>
    <li><a href="fatalities_per_state.html">Which State has the Highest Fatality Rate?</a></li>
    <li><a href="fatalities_per_day.html">Which Day of the Week is Deadliest?</a></li>
    <li><a href="fatalities_per_time.html">Which Time of Day is the Deadliest?</a></li>
    <li><a href="fatalities_trucks.html">How Many Trucks are in Fatal Crashes</a></li>
    <li><a href="fatalities_speed_zones.html">Which Speed Zones are the Deadliest?</a></li>
    <li><a href="fatalities_gender.html">Does Your Gender Increase the Risk of Dying?</a></li>
    <li><a href="fatalities_age.html">Does Your Age Have Anything to do With Death-rates?</a></li>
    <li><a href="fatalities_user.html">Fatality Data</a></li>
</ul>'''))
out.close()

Which would create:

<h1>Road Fatalities in Australia</h1>
<p>On this site you will find road fatality statistical data.</p>
<p>...</p>
<p>...</p>
<ul>
    <li><a href="fatalities_per_year.html">A Look at Death Rates Over the Years</a></li>
    <li><a href="fatalities_per_state.html">Which State has the Highest Fatality Rate?</a></li>
    <li><a href="fatalities_per_day.html">Which Day of the Week is Deadliest?</a></li>
    <li><a href="fatalities_per_time.html">Which Time of Day is the Deadliest?</a></li>
    <li><a href="fatalities_trucks.html">How Many Trucks are in Fatal Crashes</a></li>
    <li><a href="fatalities_speed_zones.html">Which Speed Zones are the Deadliest?</a></li>
    <li><a href="fatalities_gender.html">Does Your Gender Increase the Risk of Dying?</a></li>
    <li><a href="fatalities_age.html">Does Your Age Have Anything to do With Death-rates?</a></li>
    <li><a href="fatalities_user.html">Fatality Data</a></li>
</ul>

given that

page = """{heading}
{contents1}
{contents2}
{contents3}
{link}"""

Comments

0
heading = "<h1>Road Fatalities in Australia</h1>"
contents1 = "<p>On this site you will find road fatality statistical data.</p>"
link = ['<a href="fatalities_per_year.html">A Look at Death Rates Over the Years</a>',
'<a href="fatalities_per_state.html">Which State has the Highest Fatality Rate?</a>',
'<a href="fatalities_per_day.html">Which Day of the Week is Deadliest?</a>',
'<a href="fatalities_per_time.html">Which Time of Day is the Deadliest?</a>',
'<a href="fatalities_trucks.html">How Many Trucks are in Fatal Crashes</a>',
'<a href="fatalities_speed_zones.html">Which Speed Zones are the Deadliest?</a>',
'<a href="fatalities_gender.html">Does Your Gender Increase the Risk of Dying?</a>',
'<a href="fatalities_age.html">Does Your Age Have Anything to do With Death-rates?</a>',
'<a href="fatalities_user.html">Fatality Data</a>']
with open("this.html", "w") as myfile:
    myfile.write(heading)
    myfile.write(contents1)
    for l in link:
        myfile.write(l)

OUTPUT

<h1>Road Fatalities in Australia</h1><p>On this site you will find road fatality statistical data.</p><a href="fatalities_per_year.html">A Look at Death Rates Over the Years</a><a href="fatalities_per_state.html">Which State has the Highest Fatality Rate?</a><a href="fatalities_per_day.html">Which Day of the Week is Deadliest?</a><a href="fatalities_per_time.html">Which Time of Day is the Deadliest?</a><a href="fatalities_trucks.html">How Many Trucks are in Fatal Crashes</a><a href="fatalities_speed_zones.html">Which Speed Zones are the Deadliest?</a><a href="fatalities_gender.html">Does Your Gender Increase the Risk of Dying?</a><a href="fatalities_age.html">Does Your Age Have Anything to do With Death-rates?</a><a href="fatalities_user.html">Fatality Data</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.