2017-09-29 1 views
0

SECファイルから.xmlデータを取得しようとしています。 second table. しかし、もし私が.xmlを持っていないページに行った場合は、htmlのverを使用したいと思います。first & only table. 誰かが私に2番目のテーブルがある場合は最初のテーブルを反復またはスキップする方法を理解してください。 1つしか存在しない場合は、最初のテーブルの最初のa ['href']を取得しますか?2つのテーブルが存在する場合、Pythonicのカウントとスクレイピングによって最初のテーブルがスキップされる

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"] 
あなたが最後のテーブルを取得するには、リストのインデックス-1を使用することができますので、あなたは常に、両方のケースで最後のテーブルからの情報が必要

答えて

1

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 
+0

感謝[CtheSky](https://でのstackoverflow .com/users/6779007/cthesky) –

関連する問題