Reputation: 799
I am trying to extract just the text from an anchor tag. I understand that find_all
returns a ResultSet object and that I need to iterate over it, however, I can't seem to get it work. My code below:
for all in soup.find("div", {"id": "info-area"}):
Name = all.find_all("a")
#print(Name) # Returns everything
#print(Name.text) # throws error
for the_name in Name:
print(Name.text) # throws error
Obviously, I'm doing something wrong but not quite sure what?
Upvotes: 0
Views: 2348
Reputation: 811
@Maverick,
"Name" is the list, which has all 'a' tag elements.
You can not use .text attribute on a list.
To my understanding, the below code should work
for the_name in Name:
print(the_name.text)
Your approach is correct. But the mistake you did in the above loop is, for each element in the list you have call the element("the_name") not the list ("Name")
Upvotes: 0
Reputation: 6556
The problem is the first for loop, change your code to:
all_div = soup.find("div", {"id": "info-area"}) #find div with id = info-area
Name = all_div.find_all("a") # under all_div find all a
for the_name in Name: #loop through each a
print(the_name.text) #print each a text
Upvotes: 1