2016-11-06 11 views
0

ウェブページのURLを再帰的に取得し、その結果をリストで取得したいと考えています。BeautifulSoup Soup Recursive

これは私が使用しているコードです:私は、URLのcatalog_url内を得る最初のループでは

catalog_url = "http://nomads.ncep.noaa.gov:9090/dods/gfs_0p25/" 

from bs4 import BeautifulSoup # conda install -c asmeurer beautiful-soup=4.3.2 
import urllib2 
from datetime import datetime 

html_page = urllib2.urlopen(catalog_url) 
soup = BeautifulSoup(html_page) 

urls_day = [] 
for link in soup.findAll('a'): 
    if datetime.today().strftime('%Y') in link.get('href'): # String contains today's year in name 
     print link.get('href') 
     urls_day.append(link.get('href')) 

urls_final = [] 
for run in urls_day: 
    html_page2 = urllib2.urlopen(run) 
    soup2 = BeautifulSoup(html_page2) 
    for links in soup2.findAll('a'): 
     if datetime.today().strftime('%Y') in soup2.get('a'): 
      print links.get('href') 
      urls_final.append(links.get('href')) 

を。 urls_dayは、現在の年の文字列を含むurlを持つリストオブジェクトです。

第二のループは、次の出力で失敗します。

<a href="http://nomads.ncep.noaa.gov:9090/dods">GrADS Data Server</a> 
Traceback (most recent call last): 
    File "<stdin>", line 6, in <module> 
TypeError: argument of type 'NoneType' is not iterable 

urls_finalは、URLの私の関心のを含むリストオブジェクトでなければなりません。

これを解決する方法はありますか?私は再帰で美しいスープの同様の記事をチェックしましたが、私はいつも同じ 'NoneType'レスポンスを取得します。

+0

おそらくsoup2.findAll( 'A')で '場合datetime.today()はstrftime( '%Y')必要があります。代わりに' '... soup2.get( 'A')のを'。 –

答えて