gpt4 book ai didi

Python Beautifulsoup,在标签中获取 href 标签

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

我在获取 href 时遇到问题标签,所以我的情况是这样的,
这是html文件 :

<div class="list-product with-sidebar">
<a class="frame-item" href="./produk-a.html" target="_blank" title="Produk A">

</a>
<a class="frame-item" href="./produk-b.html" target="_blank" title="Produk B">

</a>
</div>
所以这里是我的代码
    def get_category_item_list(category):
base_url = 'https://www.website.com/'
res = session.get(base_url+category)
res = BeautifulSoup(res.content, 'html.parser')
all_title = res.findAll('a', attrs={'class':'frame-item'})
data_titles = []
for title in all_title:
product_link = title.get('a')['href']
data_titles.append(product_link)
return data_titles
我想得到的是, href链接..像这样
produk-a.html
produk-b.html
当我尝试运行它时..它不会让我获得 href 上的链接,他们给出错误代码:
TypeError: 'NoneType' object is not subscriptable

最佳答案

我相信你的问题在于这一行:

product_link = title.get('a')['href']
您已经有一个“a”元素列表,因此您可能只需要:
product_link = title['href']

关于Python Beautifulsoup,在标签中获取 href 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63449111/

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