2017-01-27 6 views
-1

max_chapterを設定し、チャプター番号がmax_chapterに達するまで機能したいと思います。次に、ブック番号が1つ増え、チャプタ番号がmax_chapterに達するまで同じ機能を実行します。この場合、2つの「while」ループを一緒にする方法は?

たとえば、第1章 - 第1章〜第20章、本は第2章となり、第2章 - 第1章〜第20章などの機能を果たします。ここで

は私が疑問持っている私のコードの一部です:だから

import requests 
from bs4 import BeautifulSoup 
import operator 

def start(max_book): 
    word_list = [] 
    book = 1 
    chapter = 1 
    while book <= max_book: 
     url = ('http://www.holybible.or.kr/B_NIV/cgi/bibleftxt.php?VR=NIV&VL=' 
       + str(book) + '&CN=' + str(chapter) + '&CV=99') 
     while chapter <= 1: 
      source_code = requests.get(url).text 
      soup = BeautifulSoup(source_code, "html.parser") 
      for bible_text in soup.findAll('font', {'class': 'tk4l'}): 
       content = bible_text.get_text() 
       words = content.lower().split() 
       for each_word in words: 
        word_list.append(each_word) 
      chapter += 1 
     else: 
      book += 1 
    print(word_list) 
start(1) 
+0

あなたがそれらを一緒に入れて何を意味するのですか? – Sayse

+0

oh ...私は本の第1章〜第20章のすべてのテキストを拾い読みし、第2章の第1章に移動して同じことをしたいと思っています。だから、str(章)とstr(本)の両方が順番に増えるべきだと思う。いいえ? –

答えて

1

IIUCでは、各書籍から最初の20章を読む必要があります。

def Readchapters(max_books,max_chapters): 
    book=1 
    chapter=1 
    while book <= max_books: 

     while chapter<=max_chapters: 
      print "reading book :",book ,"Chapter : ",chapter 
      url = 'http://www.holybible.or.kr/B_NIV/cgi/bibleftxt.php?VR=NIV&VL={}&CN={}&CV=99'.format(book, chapter) 
      source_code = requests.get(url).text 
      soup = BeautifulSoup(source_code, "html.parser") 
      ''' 
      #do your scraping here 
      ................................ 
      ................................ 
      '''  
      chapter+=1 #move to next chapter 
     book += 1 #move to next book 
     chapter=1 #reset the chapter back 
Readchapters(2,20) 

出力

reading book : 1 Chapter : 1 
reading book : 1 Chapter : 2 
reading book : 1 Chapter : 3 
reading book : 1 Chapter : 4 
reading book : 1 Chapter : 5 
reading book : 1 Chapter : 6 
reading book : 1 Chapter : 7 
reading book : 1 Chapter : 8 
reading book : 1 Chapter : 9 
reading book : 1 Chapter : 10 
reading book : 1 Chapter : 11 
reading book : 1 Chapter : 12 
reading book : 1 Chapter : 13 
reading book : 1 Chapter : 14 
reading book : 1 Chapter : 15 
reading book : 1 Chapter : 16 
reading book : 1 Chapter : 17 
reading book : 1 Chapter : 18 
reading book : 1 Chapter : 19 
reading book : 1 Chapter : 20 
reading book : 2 Chapter : 1 
reading book : 2 Chapter : 2 
reading book : 2 Chapter : 3 
reading book : 2 Chapter : 4 
reading book : 2 Chapter : 5 
reading book : 2 Chapter : 6 
reading book : 2 Chapter : 7 
reading book : 2 Chapter : 8 
reading book : 2 Chapter : 9 
reading book : 2 Chapter : 10 
reading book : 2 Chapter : 11 
reading book : 2 Chapter : 12 
reading book : 2 Chapter : 13 
reading book : 2 Chapter : 14 
reading book : 2 Chapter : 15 
reading book : 2 Chapter : 16 
reading book : 2 Chapter : 17 
reading book : 2 Chapter : 18 
reading book : 2 Chapter : 19 
reading book : 2 Chapter : 20 
+0

ワ!どうもありがとうございます。 –

0

を、私はSTR(章)とstr(書籍)が順番に増やす必要があり、両方だと思います。いいえ?

URLが新しい章番号で更新されるようにするには、内部whileループの内部にurlの構造を含めるだけです。

while book <= max_book: 
    while chapter <= 1: 
     url = 'http://www.holybible.or.kr/B_NIV/cgi/bibleftxt.php?VR=NIV&VL={}&CN={}&CV=99'.format(book, chapter) 
+0

注: 'chapter <= 1'はまだ20章すべてを取得できないように制限していますが、それは別の問題です – Sayse

+0

はい、20に変更できます。正しく動作するかどうかを確認したいと思っていました。だから、もし私はそれを1に設定し、それをstart(2)で実行します。その後、それは私に最初の2つの本の最初の章を手に入れることができるはずですが、それはまだ私に最初の本の章1を与えるだけです。 –

+0

@YunTaeHwang - この答えに応じて、while whileループとurlの順序を変更しましたか? – Sayse

関連する問題