python - Python BeautifulSoup 网址
问题描述
我想问的是,我怎样才能在 BeautifulSoup 中获取标签中的一部分 URL
这是 BeautifulSoup 返回的 href 标签:
<a href="https://www.goodsmile.info/zh/products/category/nendoroid_series/announced/2020" ping="/url?sa=t&source=web&rct=j&url=https://www.goodsmile.info/zh/products/category/nendoroid_series/announced/2020&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')
我需要修改什么?谢谢
解决方案
使用.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']
推荐阅读
- javascript - 打破请求-承诺的循环
- html - 显示来自 ASP.NET 的数据时表损坏
- javascript - HTML,Javascript:对具有相同名称的 div 元素中的值求和并将 sum 写入另一个 div 元素
- javascript - 是否可以将 HERE Map 与 firebase firestore 数据库一起使用?
- javascript - 如何使用 Typescript 对具有后备值的解构对象进行类型检查?
- java - 在android中表示时间的最佳方式是什么?
- kotlin - Kotlin - 数据类类型字符串或一些对象
- c++ - 打开多个输入文件
- bash - 获取数组元素大小
- android - Android:bootCompleted TAG 是否触发了两次?