Correct! Given that soup.find_all(class_=’items’) returns a list, in order to get the first item, all you need to do is index.
first_item = items.find(0)
Try again! Since soup.find_all(class_=’items’) returns a list, we cannot use find() as it is a string method that returns the first instance of a specified value in a string.
first_item = items.get(0)
Try again! Since soup.find_all(class_=’items’) returns a list, we cannot use get() as it is a dictionary method used to return the value of an item with specified key.
first_item = items.find[0]
Try again! Since soup.find_all(class_=’items’) returns a list, we cannot use find() as it is a string method that returns the first instance of a specified value in a string.
first_item = soup.items[0]
Try again! We already called the soup object to get items so all we need to do is index to the first item.
Try again! Attributes is not the correct way to get a dictionary of all attributes and values for a tag.
soup.link.attrs
Try again! There is no tag ’link’, instead we use tag ’a’ to find links.
soup.a.attrs
Correct! This is the correct way to get the first link tag (soup.a) and get a dictionary of all attributes and values for that link tag (.attrs).
soup.link.attributes
Try again! There is no tag ’link’, instead we use tag ’a’ to find links. Attributes is not the correct way to get a dictionary of all attributes and values for a tag.
Try again! There is no tag called ’paragraph’, instead we use tag ’p’ to find paragraphs. Also, to find a class in Beautiful Soup, it requires an underscore (class_).
all_links = soup.find_all(’p’, class_=’b-soup’)
Correct! This is the correct way to find all paragraph tags. In HTML, paragraph tags are ’p’ tags. For Beautiful Soup, to find a class, class requires an underscore (class_).
After creating an empty dictionary and getting a list of all link tags, how does one put the link_tag text as keys and the link_taghref attribute as values for the dictionary?
loop through the elements of the list and do dictionary[link_tag.text] = a.get(’href’, None)
Try again! Although the ’a’ tag is the link tag, the variable that contains the href attribute is link_tag.
loop through the elements of the list and do dictionary[link_tag.text] = a[’href’]
Try again! Although the ’a’ tag is the link tag, the variable that contains the href attribute is link_tag. Also, using the format tag[’attribute_name’] will cause an error if the tag is not there.
loop through the elements of the list and do dictionary[link_tag.text] = link_tag.get(’href’, None)
Correct! This is the correct way to create a dictionary with link_tag text as keys and href as values. Using .get(’attribute_name’, None) will not cause an error. It will set None as the default value and grab the value if there is one.
loop through the elements of the list and do dictionary[link_tag.text] = link_tag[href]
Try again! The attribute name is missing quotation marks, and using the format tag[’attribute_name’] will cause an error if the tag is not there.
url = "https://www.nytimes.com/"
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, 'html.parser')
tags = soup('img')
for tag in tags:
print(tag.get('src', None))
retrieves and displays the webpage
Try Again! Urllib retrieves the webpage but does not display it.
downloads the webpage
Try Again! This does not save files to the computer.
prints the images from ’www.nytimes.com’
Try Again! BeautifulSoup and html.parser cannot display images
prints all the ’img’ sources under ’src’ from ’www.nytimes.com’
Correct! It prints out the image sources listed under ’src’ of ’img’ tags.