简体   繁体   English

如何在 python 中提取标签 href

[英]how to extract a tag href in python

i want extract a href tag from a class, how can i do it?我想从 class 中提取一个 href 标签,我该怎么做? this is my code:这是我的代码:

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:
        link = a_tag.get('href')
        print(link)
manga_name = input("inserisci il nome del manga: ")
search_manga(manga_name)

this is the output:这是 output:

AttributeError: ResultSet object has no attribute 'get'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

how can I solve it?我该如何解决?

Looking at the HTML response from your query, it appears that there is only one element with class 'openManga'.查看您查询的 HTML 响应,似乎只有一个元素带有 class 'openManga'。 Therefore, this can be simplified as follows:-因此,这可以简化如下:-

from bs4 import BeautifulSoup as BS
import requests


def getHref(title):
    with requests.Session() as sess:
        r = sess.get('https://beta.mangaeden.com/it/it-directory/',
                     params={'title': title})
        r.raise_for_status()
        soup = BS(r.text, 'html.parser')
        a = soup.find_all('a', class_='openManga')
        if a:
            return a[0].get('href', None)


print(getHref('Berserk'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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