Reputation: 15
I'm a python beginner and im quiet stuck in a exercise.
i'm using beautiful soup for parsing web pages.
i have to update my link with the index position that i got u = tags[s]
. i know that i must use a .get() that reads the variable that stores the html line which is (url).
When i try to use url1 = u.get(url)
and print. my result is None.
So how i can use the .get() proporly?
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ')
s = input("Enter position: ")
m = input("Enter count: ")
s = int(s)
m = int(m)
while m > 0:
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, 'html.parser')
tags = soup("a")
m = m - 1
#print(tags)
u = tags[s]
url1 = u.get(url)
print(url1)
Upvotes: 1
Views: 333
Reputation: 9721
Have a look at the Python docs for the dict
type.
Regarding the .get()
function it states (bold mine):
get(key[, default]) Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.
Example:
# Create a dict.
d = {'name': 'bob'}
# Index to the (non-existent) 'age' key.
d.get('age')
>>> None # Not actually printed to the console. Added for demonstration.
# Index to the 'name' key.
d.get('name')
>>> 'bob'
# Index to the (non-existent) 'age' key, using a default value.
d.get('age', 'unknown')
>>> 'unknown'
Answer:
To answer your question, if None
is returned from the .get()
call, then the key being indexed does not exist. If a default value should be returned (to keep following code from falling over), pass a value into the optional default
parameter.
Upvotes: 1