Here is the html code
[<div >
<ul >
<li>
<div >
<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 ></div>
</a>
</div>
<p ><a href="/digimon-ghost-game-episode-36" title="Digimon Ghost Game">Digimon Ghost Game</a></p>
<p >Episode 36</p>
</li>
<li>
<div >
<a href="/waccha-primagi-episode-41" title="Waccha PriMagi!">
<img alt="Waccha PriMagi!" src="https://gogocdn.net/cover/waccha-primagi.png"/>
<div ></div>
</a>
</div>
<p ><a href="/waccha-primagi-episode-41" title="Waccha PriMagi!">Waccha PriMagi!</a></p>
<p >Episode 41</p>
</li>
<li>
<div >
<a href="/one-piece-episode-1027" title="One Piece">
<img alt="One Piece" src="https://gogocdn.net/images/anime/One-piece.jpg"/>
<div ></div>
</a>
</div>
<p ><a href="/one-piece-episode-1027" title="One Piece">One Piece</a></p>
<p >Episode 1027</p>
</li>
<li>
<div >
<a href="/delicious-partyprecure-episode-21" title="Delicious Party♡Precure">
<img alt="Delicious Party♡Precure" src="https://gogocdn.net/cover/delicious-partyprecure.png"/>
<div ></div>
</a>
</div>
<p ><a href="/delicious-partyprecure-episode-21" title="Delicious Party♡Precure">Delicious Party♡Precure</a></p>
<p >Episode 21</p>
</li>
<li>
<div >
<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 ></div>
</a>
</div>
<p ><a href="/kingdom-4th-season-episode-17" title="Kingdom 4th Season">Kingdom 4th Season</a></p>
<p >Episode 17</p>
</li>
<li>
<div >
<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 ></div>
</a>
</div>
<p ><a href="/kingdom-4th-season-chinese-name-episode-17" title="Kingdom 4th Season (Chinese Name)">Kingdom 4th Season (Chinese Name)</a></p>
<p >Episode 17</p>
</li>
<li>
<div >
<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 ></div>
</a>
</div>
<p ><a href="/saikin-yatotta-maid-ga-ayashii-episode-2" title="Saikin Yatotta Maid ga Ayashii">Saikin Yatotta Maid ga Ayashii</a></p>
<p >Episode 2</p>
</li>
<li>
<div >
<a href="/extreme-hearts-episode-4" title="Extreme Hearts">
<img alt="Extreme Hearts" src="https://gogocdn.net/cover/extreme-hearts.png"/>
<div ></div>
</a>
</div>
<p ><a href="/extreme-hearts-episode-4" title="Extreme Hearts">Extreme Hearts</a></p>
<p >Episode 4</p>
</li>
<li>
<div >
<a href="/engage-kiss-episode-5" title="Engage Kiss">
<img alt="Engage Kiss" src="https://gogocdn.net/cover/engage-kiss.png"/>
<div ></div>
</a>
</div>
<p ><a href="/engage-kiss-episode-5" title="Engage Kiss">Engage Kiss</a></p>
<p >Episode 5</p>
</li>
<li>
<div >
<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 ></div>
</a>
</div>
<p ><a href="/kakkou-no-iinazuke-episode-14" title="Kakkou no Iinazuke">Kakkou no Iinazuke</a></p>
<p >Episode 14</p>
</li>
<li>
<div >
<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 ></div>
</a>
</div>
<p ><a href="/utawarerumono-futari-no-hakuoro-episode-6" title="Utawarerumono: Futari no Hakuoro">Utawarerumono: Futari no Hakuoro</a></p>
<p >Episode 6</p>
</li>
<li>
<div >
<a href="/lycoris-recoil-episode-5" title="Lycoris Recoil">
<img alt="Lycoris Recoil" src="https://gogocdn.net/cover/lycoris-recoil.png"/>
<div ></div>
</a>
</div>
<p ><a href="/lycoris-recoil-episode-5" title="Lycoris Recoil">Lycoris Recoil</a></p>
<p >Episode 5</p>
</li>
<li>
<div >
<a href="/musashino-episode-5" title="Musashino!">
<img alt="Musashino!" src="https://gogocdn.net/cover/musashino.png"/>
<div ></div>
</a>
</div>
<p ><a href="/musashino-episode-5" title="Musashino!">Musashino!</a></p>
<p >Episode 5</p>
</li>
<li>
<div >
<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 ></div>
</a>
</div>
<p ><a href="/shoot-goal-to-the-future-episode-5" title="Shoot! Goal to the Future">Shoot! Goal to the Future</a></p>
<p >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))
CodePudding user response:
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)
CodePudding user response:
Your line p_tags = soup.find_all('p', class_='name')
returns a result set (e.g., 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)