Python - 'NoneType' 对象不可订阅?
Python - 'NoneType' object is not subscriptable?
from bs4 import BeautifulSoup
import requests
url = "https://www.wikipedia.com/wiki/computer"
response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, "lxml")
results = soup.find_all("a")
for results in results:
if results.get("href") [:5] == "https":
print(results.get("href"))
此代码returns出现以下错误:
TypeError: 'NoneType' object is not subscriptable
我试过将 [:5] 放在 () 中,这会停止错误但不会输出超链接。我哪里做错了?
始终检查函数调用的 return 值。
all_results = soup.find_all("a")
for results in all_results:
result = results.get("href")
if result is None:
pass # put a breakpoint here to see all the results which do not contain a "href" tag
elif result.startswith("https"):
print(result)
from bs4 import BeautifulSoup
import requests
url = "https://www.wikipedia.com/wiki/computer"
response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, "lxml")
results = soup.find_all("a")
for results in results:
if results.get("href") [:5] == "https":
print(results.get("href"))
此代码returns出现以下错误:
TypeError: 'NoneType' object is not subscriptable
我试过将 [:5] 放在 () 中,这会停止错误但不会输出超链接。我哪里做错了?
始终检查函数调用的 return 值。
all_results = soup.find_all("a")
for results in all_results:
result = results.get("href")
if result is None:
pass # put a breakpoint here to see all the results which do not contain a "href" tag
elif result.startswith("https"):
print(result)