1
我想提取所有的網址,並將所有這些網址放入列表中。但是,當我運行代碼時,它顯示一條錯誤消息:"tag[key] returns the value of the 'key' attribute for the tag, and throws an exception if it's not there."
我想知道如何解決此問題。 我的代碼如下:無法將網址放入列表(BeautifulSoup)
import urllib.request
from bs4 import BeautifulSoup
r = 'https://stackoverflow.com/'
openedUrl = urllib.request.urlopen(r)
soup = BeautifulSoup(openedUrl, 'lxml')
aa = soup.find_all('a')
href = []
for a in aa:
href.append(a['href'])
print(href)
它的工作原理。非常感謝! – tzu