2016-07-07 6 views
0

次のURLリンクに移動する必要があります(各ページには抽出する必要がある行が約20行あり、次の結果に次の結果を追加する必要があります)。次のURL)。複数のページをループしてデータをスクラップすることができません

約360のURLがあり、それらをすべて実行してデータを抽出したいと考えています。私のコードは以下の通りです。後でCSVファイルに書きたいと思います。私がPythonに慣れていないので、どんな提案も高く評価されます。

 from urlparse import urljoin 
    import requests 
    from bs4 import BeautifulSoup 
    import csv 

    base_url = 'http://cricket.inhs.uiuc.edu/edwipweb/FMPro?-db=nvpassoc.fp5&-format=nvp_search_results.htm&-lay=web%20form&-max=20&-findall=' 
    list_of_rows = [] 

    next_page = 'http://cricket.inhs.uiuc.edu/edwipweb/FMPro?-db=nvpassoc.fp5&-format=nvp_search_results.htm&-lay=web%20form&-max=20&-skip=20&-findall=' 

    while True: 
     soup = BeautifulSoup(requests.get(next_page).content) 
     soup.findAll('table')[1].findAll('tr') 
      for row in soup.findAll('table')[1].findAll('tr'): 
      list_of_cells = [] 
       for cell in row.findAll('p'): 
       text = cell.text.replace(' ','') 
       list_of_cells.append(text) 
      list_of_rows.append(list_of_cells) 

    try: 
     next_page = urljoin(base_url, soup.select('/FMPro?-db=nvpassoc.fp5&-format=nvp_search_results.htm&-lay=web%20form&-max=20&-skip=20&-findall=')[1].get('href')) 
    except IndexError: 
    break 


    print list_of_rows 

    outfile = open("./trialpage.csv","wb") 
    writer = csv.writer(outfile) 
    writer.writerows(list_of_rows) 
+0

whileループ内にtry/exceptブロックを置く必要があります。また、最大値とスキップ値を変更するたびにnext_pageを更新する必要があります。 –

+0

whileループ内です。私が抱えている問題は、ページの最初の20行を印刷しているだけで、次のWebページに移動していないということです。 –

+0

あなたのwhileループにはありません。 forループの下に移動してください –

答えて

0

コードにいくつか変更を加えました。私はスキップと呼ばれる変数を使って元のURLを設定します。

from urlparse import urljoin 
import requests 
from bs4 import BeautifulSoup 
import csv 

list_of_rows = [] 

skip = 0 
next_page = 'http://cricket.inhs.uiuc.edu/edwipweb/FMPro?-db=nvpassoc.fp5&-format=nvp_search_results.htm&-lay=web%20form&-max=20&-skip=' + str(skip) + '&-findall=' 
print next_page 
while True: 
    soup = BeautifulSoup(requests.get(next_page).content) 
    soup.findAll('table')[1].findAll('tr') 
    for row in soup.findAll('table')[1].findAll('tr'): 
     list_of_cells = [] 
     for cell in row.findAll('p'): 
      text = cell.text.replace(' ','') 
      list_of_cells.append(text) 
     list_of_rows.append(list_of_cells) 

    try: 
     skip += 20 
     if skip > 300: 
      break 
     next_page = 'http://cricket.inhs.uiuc.edu/edwipweb/FMPro?-db=nvpassoc.fp5&-format=nvp_search_results.htm&-lay=web%20form&-max=20&-skip=' + str(skip) + '&-findall=' 
     print next_page 
    except IndexError as e: 
     print e 
     break 


# print list_of_rows 

outfile = open("./trialpage.csv","wb") 
writer = csv.writer(outfile) 
writer.writerows(list_of_rows) 

て20ずつインクリメントされますスキップあなたが画面表示によって限定されるものではないので、あなたは大きな塊を取ることができると私はそれがより速く働くだろうと思います。最大値= 200を試した後、ステップ単位で200を増やす

+0

ありがとう@joel。最終ページに達すると、コードは引き続きループし、最後のページは永遠に繰り返されます。とにかく私は最大のスキップを停止するように設定することができますか? –

+0

私は自分自身に気づいた。手動でページにアクセスし、コレクションを終了させる何かを見つけ出すことをお勧めします。コードに私の編集を参照してください –

+0

素晴らしい、その作品。ただし、スクリプトがスクレイピングしている各レコードは、基本的にそれぞれ7回(この練習では列数)繰り返されます。それには –

関連する問題