source.first()只返回第一个元素。
引用的first()方法的源代码:def find(self, name=None, attrs={}, recursive=True, text=None, **kwargs):
"""Return only the first child of this Tag matching the given criteria."""
r = None
l = self.find_all(name, attrs, recursive, text, 1, **kwargs)
if l:
r = l[0]
return r
findChild = find
在提取表之后,它的类名是wikitable sortable。
所以按照上面的代码,它返回None。
所以你可能想把你的代码改成。。。from bs4 import BeautifulSoup
from urllib.request import urlopen
url = 'https://simple.wikipedia.org/wiki/List_of_U.S._states'
web = urlopen(url)
source = BeautifulSoup(web, 'html.parser')
table = source.find('table', class_='wikitable')
abbs = table.find_all('b')
abbs_list = [i.get_text().strip() for i in abbs]
print(abbs_list)
我希望它能回答你的问题。:)