首页 > 解决方案 > Python BeautifulSoup 网址

问题描述

我想问的是,我怎样才能在 BeautifulSoup 中获取标签中的一部分 URL

这是 BeautifulSoup 返回的 href 标签:

<a href="https://www.goodsmile.info/zh/products/category/nendoroid_series/announced/2020" ping="/url?sa=t&amp;source=web&amp;rct=j&amp;url=https://www.goodsmile.info/zh/products/category/nendoroid_series/announced/2020&amp;ved=2ahUKEwjT-_Gy4PzsAhWIyosBHd4ZAAkQFjBvegQIYhAC">

但我只想说:

https://www.goodsmile.info/zh/products/category/nendoroid_series/announced/2020

我能怎么做?

这是我的一些代码:

for hit in soup.find_all(class_='g'):
    Hit_title = hit.find('h3')
    URL=hit.find(class_='yuRUbf').find('a', href=True).get('href')

我需要修改什么?谢谢

标签: pythonbeautifulsoup

解决方案


使用.attrs['href']代替.get('href')

for hit in soup.find_all(class_='g'):
    Hit_title = hit.find('h3')
    URL=hit.find(class_='yuRUbf').find('a', href=True).attrs['href']

推荐阅读