python 从维基百科页面中截取所有表格标题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 从维基百科页面中截取所有表格标题相关的知识,希望对你有一定的参考价值。

import urllib.request

from bs4 import BeautifulSoup

WIKIPEDIA_URL = 'http://en.wikipedia.org/wiki/'
TARGET_URL = WIKIPEDIA_URL + 'list_of_highest_mountains'
INDENT = ' ' * 2

opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
page = opener.open(TARGET_URL)
soup = BeautifulSoup(page)

print("PAGE:", TARGET_URL)
for table_num, table in enumerate(soup('table')):
    print()
    print("{}Table.{}:".format(INDENT, table_num + 1))
    for header_num, table_header in enumerate(table('th')):
        header = table_header.get_text()
        print("{}{}. {}".format(INDENT * 2, header_num, header))

以上是关于python 从维基百科页面中截取所有表格标题的主要内容,如果未能解决你的问题,请参考以下文章