from bs4 import BeautifulSoup
from urllib.request import urlopen
webpage = urlopen('https://en.wikipedia.org/wiki/List_of_largest_banks')
bs = BeautifulSoup(webpage,'html.parser')
print(bs)
spanList= bs.find_all('span',{'class':'flagicon'})
for span in spanList:
print(span.a['title'])
虽然其打印了第一个表中的国家列表,但打印后出现了错误:
Traceback (most recent call last):
File "C:/Users/Jegathesan/Desktop/python programmes/scrape5.py", line 10, in <module>
print(span.a['title'])
TypeError: 'NoneType' object is not subscriptable
最初的代码是检查所有解析的html中的span标记
修改后的代码将获得在html中找到的所有表标记并存储在列表中
使用语句获取特定表(即第一个表)的span标记
输出:
相关问题 更多 >
编程相关推荐