Python获取类中找到的每个ahref链接的标题状态,并打印带有状态代码的链接

Python获取类中找到的每个ahref链接的标题状态,并打印带有状态代码的链接,python,beautifulsoup,Python,Beautifulsoup,我正在尝试提取某个类中我的html中找到的所有href链接,并将它们与服务器标题状态一起打印 要查找每个ahref链接,我有以下内容 for href in soup.find_all('section', class_='holder'): for a in href.find_all('a'): if a.get('href') == '/': continue else: print(a.get('hr

我正在尝试提取某个类中我的html中找到的所有href链接,并将它们与服务器标题状态一起打印

要查找每个ahref链接,我有以下内容

for href in soup.find_all('section', class_='holder'):
    for a in href.find_all('a'):
        if a.get('href') == '/':
            continue
        else:
            print(a.get('href'))
这会打印所有url,但我还想打印每个url旁边的每个url的服务器头状态

我试过这样的方法,但不起作用:

for href in soup.find_all('section', class_='holder'):
    for a in href.find_all('a'):
        headers = requests.head('a')
        if a.get('href') == '/':
            continue
        else:
            print(a.get('href'), (headers))
我期望的输出是:

https://www.exampleurlone.com/urlone 200
https://www.exampleurlone.com/urltwo 200
https://www.exampleurlone.com/urlthree 404

可以这样做吗?

您可能需要
状态\u代码

Ex:

for href in soup.find_all('section', class_='holder'):
    for a in href.find_all('a'):
        if a.get('href') == '/':
            continue
        else:
            headers = requests.head(a.get('href'))
            print(a.get('href'), (headers.status_code))

太好了。非常感谢。