简体   繁体   中英

AttributeError : ResultSet object has no attribute 'find_all'

Here is the html code

 [<div class="last_episodes loaddub"> <ul class="items"> <li> <div class="img"> <a href="/digimon-ghost-game-episode-36" title="Digimon Ghost Game"> <img alt="Digimon Ghost Game" src="https://gogocdn.net/cover/digimon-ghost-game.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/digimon-ghost-game-episode-36" title="Digimon Ghost Game">Digimon Ghost Game</a></p> <p class="episode">Episode 36</p> </li> <li> <div class="img"> <a href="/waccha-primagi-episode-41" title="Waccha PriMagi:"> <img alt="Waccha PriMagi." src="https://gogocdn.net/cover/waccha-primagi.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/waccha-primagi-episode-41" title="Waccha PriMagi:">Waccha PriMagi.</a></p> <p class="episode">Episode 41</p> </li> <li> <div class="img"> <a href="/one-piece-episode-1027" title="One Piece"> <img alt="One Piece" src="https://gogocdn.net/images/anime/One-piece.jpg"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/one-piece-episode-1027" title="One Piece">One Piece</a></p> <p class="episode">Episode 1027</p> </li> <li> <div class="img"> <a href="/delicious-partyprecure-episode-21" title="Delicious Party♡Precure"> <img alt="Delicious Party♡Precure" src="https://gogocdn.net/cover/delicious-partyprecure.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/delicious-partyprecure-episode-21" title="Delicious Party♡Precure">Delicious Party♡Precure</a></p> <p class="episode">Episode 21</p> </li> <li> <div class="img"> <a href="/kingdom-4th-season-episode-17" title="Kingdom 4th Season"> <img alt="Kingdom 4th Season" src="https://gogocdn.net/cover/kingdom-4th-season.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/kingdom-4th-season-episode-17" title="Kingdom 4th Season">Kingdom 4th Season</a></p> <p class="episode">Episode 17</p> </li> <li> <div class="img"> <a href="/kingdom-4th-season-chinese-name-episode-17" title="Kingdom 4th Season (Chinese Name)"> <img alt="Kingdom 4th Season (Chinese Name)" src="https://gogocdn.net/cover/kingdom-4th-season-chinese-name.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/kingdom-4th-season-chinese-name-episode-17" title="Kingdom 4th Season (Chinese Name)">Kingdom 4th Season (Chinese Name)</a></p> <p class="episode">Episode 17</p> </li> <li> <div class="img"> <a href="/saikin-yatotta-maid-ga-ayashii-episode-2" title="Saikin Yatotta Maid ga Ayashii"> <img alt="Saikin Yatotta Maid ga Ayashii" src="https://gogocdn.net/cover/saikin-yatotta-maid-ga-ayashii.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/saikin-yatotta-maid-ga-ayashii-episode-2" title="Saikin Yatotta Maid ga Ayashii">Saikin Yatotta Maid ga Ayashii</a></p> <p class="episode">Episode 2</p> </li> <li> <div class="img"> <a href="/extreme-hearts-episode-4" title="Extreme Hearts"> <img alt="Extreme Hearts" src="https://gogocdn.net/cover/extreme-hearts.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/extreme-hearts-episode-4" title="Extreme Hearts">Extreme Hearts</a></p> <p class="episode">Episode 4</p> </li> <li> <div class="img"> <a href="/engage-kiss-episode-5" title="Engage Kiss"> <img alt="Engage Kiss" src="https://gogocdn.net/cover/engage-kiss:png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/engage-kiss-episode-5" title="Engage Kiss">Engage Kiss</a></p> <p class="episode">Episode 5</p> </li> <li> <div class="img"> <a href="/kakkou-no-iinazuke-episode-14" title="Kakkou no Iinazuke"> <img alt="Kakkou no Iinazuke" src="https://gogocdn.net/cover/kakkou-no-iinazuke.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/kakkou-no-iinazuke-episode-14" title="Kakkou no Iinazuke">Kakkou no Iinazuke</a></p> <p class="episode">Episode 14</p> </li> <li> <div class="img"> <a href="/utawarerumono-futari-no-hakuoro-episode-6" title="Utawarerumono: Futari no Hakuoro"> <img alt="Utawarerumono: Futari no Hakuoro" src="https://gogocdn.net/cover/utawarerumono-futari-no-hakuoro.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/utawarerumono-futari-no-hakuoro-episode-6" title="Utawarerumono: Futari no Hakuoro">Utawarerumono. Futari no Hakuoro</a></p> <p class="episode">Episode 6</p> </li> <li> <div class="img"> <a href="/lycoris-recoil-episode-5" title="Lycoris Recoil"> <img alt="Lycoris Recoil" src="https://gogocdn.net/cover/lycoris-recoil.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/lycoris-recoil-episode-5" title="Lycoris Recoil">Lycoris Recoil</a></p> <p class="episode">Episode 5</p> </li> <li> <div class="img"> <a href="/musashino-episode-5" title="Musashino!"> <img alt="Musashino!" src="https://gogocdn.net/cover/musashino.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/musashino-episode-5" title="Musashino!">Musashino!</a></p> <p class="episode">Episode 5</p> </li> <li> <div class="img"> <a href="/shoot-goal-to-the-future-episode-5" title="Shoot! Goal to the Future"> <img alt="Shoot! Goal to the Future" src="https://gogocdn.net/cover/shoot-goal-to-the-future.png"/> <div class="type ic-SUB"></div> </a> </div> <p class="name"><a href="/shoot-goal-to-the-future-episode-5" title="Shoot! Goal to the Future">Shoot! Goal to the Future</a></p> <p class="episode">Episode 5</p>

whenever i try to get the a tags from the p tags i get this error. Here's the code

from urllib import response
from venv import create
from bs4 import BeautifulSoup
import requests

url = "https://gogoanime.gg?page=1"

req = requests.get(url)
Response = req.content
soup = BeautifulSoup(Response, 'html.parser')


p_tags = soup.find_all('p', class_='name')  
a_tags = p_tags.find_all('a')
for link in a_tags:
    links = link.get('href')
    print(links)

I have tried this but it still doesn't fix the error. Any help will be appreciated and thanks in advance.

p_tags = p_tags = soup.find_all('p', class_='name')  
for a_tags in p_tags.find_all('a')
    print(a_tags)

and with this i get the a tags but when i try to get the href i get the same error and and even i get the href somehow it's only the last href while there should be 19 before it.

p_tags = soup.find_all('p', class_='name')  
for a in p_tags:
    a_tags = a.find_all('a')
    print(len(a_tags))

p_tags = soup.find_all('p', class_='name') this return a list. List doesn't have a find_all() method. iterate over the items in p_tags to call find_all() .

for ptag in p_tags:
    a_tags = ptag.find_all('a')
    for link in a_tags:
        links = link.get('href')
        print(links)

Your line p_tags = soup.find_all('p', class_='name') returns a result set (eg, a collection of matching elements). You can't call find_all on a result set. You need to iterate over the result set.

So you could try something like this:

from urllib import response
from venv import create
from bs4 import BeautifulSoup
import requests

url = "https://gogoanime.gg?page=1"
req = requests.get(url)
Response = req.content
soup = BeautifulSoup(Response, 'html.parser')


p_tags = soup.find_all('p', class_='name')  # returns result set
a_tags = []
for p_tag in p_tags:  # iterate over result set
    a_tags.append(p_tag.find('a', href=True))  # append `a` tag to list

links = []
for a_tag in a_tags:
    links.append(a_tag.get('href'))  # get `href` value for each `a` tag
    
print(links)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM