2016-04-27 2 views
0
from bs4 import BeautifulSoup 
import urllib 
import json 
import os 

jaren = [str("2012"), str("2010"), str("2006"), str("2003"),str("2002"), str("1998"), str("1994"), str("1989"), str("1986"), str("1982"), str("1981"), str("1977"), str("1972"), str("1971"), str("1967"), str("1963"), str("1959"), str("1956")] 
DESIRED_COLUMNS = {1, 2, 5} #scrapes only afk, aantal & zetels 
verkiezingsData = [] 

filename = raw_input('Enter a filename: ') or 'data.json' 

#open file and open json array 
with open(filename, "w") as file: 
    file.write("[{") 
for Jaargetal in jaren: 

    #url source 
    r = urllib.urlopen("http://www.nlverkiezingen.com/TK" + Jaargetal +".html").read() 
    soup = BeautifulSoup(r, "html.parser") 
    tables = soup.find_all("table") 
    for table in tables: 
     header = soup.find_all("h1")[0].getText() 

     #print header 
     with open(filename, "a+") as file: 
       file.write("\"%s\": [" % header) #header as beginning json 
     trs = table.find_all("tr")[0].getText() 
     del verkiezingsData[:] #clear list before adding new data 


     #add the 3 columns to a list 
     for tr in table.find_all("tr")[:22]: #22 aantal columns van top till bottom 
      for index, val in enumerate(tr.find_all('td')): 
        if index in DESIRED_COLUMNS: #linkt naar desired columns bovenin 
         verkiezingsData.append(val.getText().strip()) 


     #json array van de 3 vallues 
     for a, b, c in zip(verkiezingsData[::3],  verkiezingsData[1::3], verkiezingsData[2::3]): #link naar desired columns 1,2,5 
      data2 = {'afk':a,"aantal":b, "zetels":c} 

      #file writing 
      with open(filename, 'a') as outfile: 
       json.dump(data2, outfile) 
        outfile.write(",") 


     #open file, delete last comma and close array 
     with open(filename, 'ab+') as file: 
       file.seek(-1, os.SEEK_END) 
       file.truncate() 
       file.write("],") 

#open file, delete last comma, and close array 
with open(filename, 'r+b') as file: 
    file.seek(-1, os.SEEK_END) 
    file.truncate() 
    file.write("}]") 

#open file and pretty print json data 
with open(filename, 'r') as file: 
    prettydata = json.load(file) 
with open(filename, 'w') as file: 
    json.dump(prettydata, file, sort_keys=True, indent=4, separators=(',', ': ')) 

白線で停止nlverkiezingen.comスクレイピングデータは> Iから掻き取るスクレーパを行っ

がJSONファイルとして保存します

"Tweede-Kamerverkiezingen - 12 september 2012": [ 
      { 
       "aantal": "Aantal", 
       "afk": "Afk.", 
       "zetels": "Zetels" 
      }, 
      { 
       "aantal": "2504948", 
       "afk": "VVD", 
       "zetels": "41" 
      }, 

最初の行がある:Aantal/AFK/Zetels。 私はこれを削ってはいけません。

どうすれば変更できますか? 2番目の行から掻き出しが始まること

2番目のことは、最後の行がどこでも違うことです。時には20行目、15行目。

どうすれば変更できますか?それは掻き集めているときに彼が白い/空の行を見たときに掻き取りが終わるのですか?それが正しい取得する場合

答えて

0

は知らない、それだけで推測ですが、多分

for tr in table.find_all("tr")[1:22] 

のようなものは、最初の行をスキップしますか?

0

最初の行は、Aantal/Afk/Zetelsです。私はこれを削ってはいけません。

for tr in table.find_all("tr")[1:22]:

for tr in table.find_all("tr")[:22]:を交換Pythonはゼロベースのインデックスを持っているので、1は、テーブルの2行目を指します。

どうすれば変更できますか?それは掻き集めているときに彼が白い/空の行を見たときに掻き取りが終わるのですか?

空のテーブルセルで u"\xa0" Python文字列としてBeautifulSoupによって解析されるであろう。各行の最初のタグの内容をチェックし、それをその値と比較して、ループを抜け出すために使用します。

+0

私は で何をする必要があるのか​​分からないかもしれません。私は編集する必要があるコードの種類を教えてください。 @ sahukどこの特定の行で終了する必要があるそれぞれの "年/ウェブサイト"に私は言うことができますか?今私は1:22でそれを終了することができます – Danisk

関連する問題