Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Soup('A')
#1
Hello,

I've been going through an introductory Python book that includes some material on web scraping using BeautifulSoup. My question is about the final three lines in the below code:

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 = "https://docs.python.org"
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')

# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
     print(tag.get('href', None))
I understand what the code is doing (and it works on my computer) but I'm curious about what's going on in the line "tags = soup('a')." The get() method used in the for loop on the next two lines suggests that "soup('a')" is referring to a dictionary. But if that were the case, shouldn't the code be written with square brackets as "tags = soup['a']"? Also, when I print tags the output I get indicates to me that tags is a list (the output starts with a square bracket and ends with a square bracket).

I tried looking through the BeautifulSoup documentation for any insights on this but am still unclear.

Thanks in advance for any help.
Reply


Messages In This Thread
Soup('A') - by new_coder_231013 - Aug-31-2022, 12:09 PM
RE: Soup('A') - by Larz60+ - Aug-31-2022, 12:23 PM
RE: Soup('A') - by new_coder_231013 - Sep-12-2022, 12:02 PM
RE: Soup('A') - by Gaurav_Kumar - Aug-09-2023, 11:49 AM
RE: Soup('A') - by Pubfonts - Aug-09-2023, 01:11 PM
RE: Soup('A') - by Pubfonts - Aug-10-2023, 09:24 AM
RE: Soup('A') - by Pubfonts - Aug-12-2023, 10:55 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020