python - Python - 试图将hrefs放入可用列表中
问题描述
我正在 Python (noobie) 中使用 libary BeautifulSoup,并且试图将 href 的元素放入我可以保存和使用的字符串列表中。但我只能从 for 循环中获得列表中的一个。有人可以帮忙吗?我不拒绝解释:)
a_counter = 0
for link in soup.find_all('a', class_="MuiGrid-root", href=True):
lst = [range(a_counter)]
lst[0] = link.get('href')
print(lst)
a_counter += 1 #not in use becaouse lst[a_counter] = link.get('href') out of range error.
我尝试的是在列表中添加一些随机元素,错误不会出现,但它似乎不起作用。
提前致谢
解决方案
我认为您正在覆盖列表中的相同元素,因此只能获得一个 url。而不是这样做:lst[0] = link.get('href')
您可以使用lst.append(link.get('href'))
将链接附加到您的列表。
推荐阅读
- python-3.x - GRU 切换到 CuDNNGRU 出现未知输入节点错误
- sql - 返回子表
- c# - 在 Debian 上发布后无法运行 ASP.NET Core API
- javascript - ¿ 调整打开的剑道窗口的大小?
- mysql - 使用多列制作续集 WHERE IN
- r - 当数字非常小且接近零时无法检查条件
- powershell - 在 Powershell 中从 CSV 文件导出服务器服务的结果
- android - 如何在 Unity3D 中隐藏通知 OneSignal
- rest - 使用带有 RestSharp 的凭据从 TFS REST API 获取用户配置文件
- node.js - 使用 Node sdk 将模块连接到 Edge Hub 时出错 - NotConnectedError:无法获取本地颁发者证书