You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用BeautifulSoup提取a标签href出现ResultSet报错如何解决

问题解决方法

错误根源

find_all()返回的是匹配元素组成的ResultSet集合(可理解为列表),无法直接对整个集合调用仅单个元素支持的get()方法。你的代码在for循环里错误地用整个a_tag集合调用get(),而非遍历得到的单个元素。

修复代码

仅需要修改for循环内的逻辑即可,完整修复后代码如下:

from bs4 import BeautifulSoup
import requests
from pprint import pprint
def search_manga(titolo):
    i = 0
    e = 0
    base_url = "https://beta.mangaeden.com/it/it-directory/?title="
    titolo = titolo.replace(" ", "+")
    url = base_url + titolo
    r = requests.get(url)
    soup = BeautifulSoup(r.content, "html.parser")
    manga_list = soup.find(id = 'mangaList')
    a_tag = manga_list.find_all(class_='openManga')
    print(a_tag)
    a_tag_array=[]
    for link in a_tag:
        # 这里将a_tag.get改为单个元素link.get
        href = link.get('href')
        a_tag_array.append(href)
        print(href)
    # 可返回收集到的所有href
    return a_tag_array
manga_name = input("inserisci il nome del manga: ")
search_manga(manga_name)

补充说明

如果确认目标页面该class对应的a标签只有1个,也可以将find_all(class_='openManga')改为find(class_='openManga'),直接得到单个元素,就不需要循环遍历。

内容的提问来源于stack exchange,提问作者user16711557

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 14:39:03