gpt4 book ai didi

python - 从页面中获取所有链接 Beautiful Soup

转载 作者:行者123 更新时间:2023-12-04 04:58:46 26 4
gpt4 key购买 nike

我正在使用 beautifulsoup 从页面中获取所有链接。我的代码是:

import requests
from bs4 import BeautifulSoup


url = 'http://www.acontecaeventos.com.br/marketing-promocional-sao-paulo'
r = requests.get(url)
html_content = r.text
soup = BeautifulSoup(html_content, 'lxml')

soup.find_all('href')

我得到的只是:
[]

如何获得该页面上所有 href 链接的列表?

最佳答案

你在告诉 find_all查找方法 href标签, 不是 属性。

您需要找到 <a>标签,它们用于表示链接元素。

links = soup.find_all('a')

稍后您可以访问他们的 href像这样的属性:
link = links[0]          # get the first link in the entire page
url = link['href'] # get value of the href attribute
url = link.get('href') # or like this

关于python - 从页面中获取所有链接 Beautiful Soup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46490626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com