このアフガニスタンのページをスクレイピングすると、次のようなエラーが表示されました。
Traceback (most recent call last):                                                                                                                                                                                 
  File "extract_table.py", line 23, in <module>                                                                                                                                                                    
    li = dict(chunks([i.text for i in all_td], 2))                                                                                                                                                                 
ValueError: dictionary update sequence element #28 has length 1; 2 is required
しかし、アルゼンチンのページをスクレイピングすると、コードは正常に実行されます。
all_td新しいリストが返されたかどうかを確認する方法はありますか? Pythonで使用する関数を知りたいです。
この擬似コードのようなもの:
if all_td is new list,
    execute dict(chunks([i.text for i in all_td], 2))
else
    execute dict(chunks([i.text for i in areatable.findAll('td')], 2))
私が達成したいのは、コードをアフガニスタンとアルゼンチンの両方の国で実行することです。
これは私のコードです
from bs4 import BeautifulSoup                                                                                                                                                                                       
import urllib2                                                                                                                                                                                                      
import re                                                                                                                                                                                                           
url = "http://www.howtocallabroad.com/afghanistan" # argentina works fine
html_page = urllib2.urlopen(url)
soup = BeautifulSoup(html_page)
areatable = soup.find('table',{'id':'codes'})
if areatable is None:
    print "areatable is None"
else:
    d = {}
    def chunks(l, n):
            return [l[i : i + n] for i in range(0, len(l), n)]
    all_td = areatable.findAll('td')
    all_td = filter(lambda x: x.attrs == {}, all_td)
    print ">>>>> all_td=", all_td
    li = dict(chunks([i.text for i in all_td], 2))
    print ">>>>> li=", li