如果存在两个 table,则 Pythonic 计数和抓取首先跳过 table

Pythonic counting and scraping to skip first table if two tables exist

我正在尝试从 SEC 文件中获取 .xml 数据。它在 second table. 但是,如果我到达一个没有 .xml 的页面,我想要 html vers,first & only table. 有人可以帮助我了解如何迭代或跳过第一个 table 如果有两个,以及如何在第一个 table 中获得第一个 ['href'] 如果只有一个存在?

from urllib2 import urlopen
import requests
from bs4 import BeautifulSoup
tableCount = 0
linklist = [https://www.sec.gov/Archives/edgar/data/1070789/000149315217011092/0001493152-17-011092-index.htm, https://www.sec.gov/Archives/edgar/data/1592603/000139160917000254/0001391609-17-000254-index.htm]
for l in linklist:
html = urlopen(l)
soup = BeautifulSoup(html.read().decode('latin-1', 'ignore'),"lxml")    
table = soup.findAll(class_='tableFile') # works for getting all .htm links
for item in table:
    tableCount +=1
url = table[0].a["href"]
if table.count >= 1:
    url = table[1].a["href"]
else:
    url = table.a["href"]

在这两种情况下,您总是需要来自最后一个 table 的信息,因此您可以使用列表的索引 -1 来获取最后一个 table:

import requests
from bs4 import BeautifulSoup

urls = ['https://www.sec.gov/Archives/edgar/data/1070789/000149315217011092/0001493152-17-011092-index.htm',
        'https://www.sec.gov/Archives/edgar/data/1592603/000139160917000254/0001391609-17-000254-index.htm']
for url in urls:
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    tables = soup.findAll('table', class_='tableFile')

    # assume xml table always comes after html one
    table = tables[-1]
    for a in table.findAll('a'):
        print(a['href'])  # you may filter out txt or xsd here