2017-01-27 75 views
-1

我想設置max_chapter並執行功能,直到章號達到max_chapter。然後,書號增加1並通過相同的功能,直到章號達到max_chapter在這種情況下如何將兩個「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

哦......你知道我想抓住書1中第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(書)都應該按順序增加。沒有?

你只需要在內部while循環中包含url的構造,以確保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

注意:'章<= 1'仍然會限制你能夠得到的所有20章但這是另外一個問題 – Sayse

+0

是針對我可以改變20.我只是想檢查是否正常工作,,所以,如果我將它設置爲1並且用start(2)運行它。然後,它應該能夠讓我得到前兩本書的第一章,但它仍然只給了我第一本書的第一章。 –

+0

@YunTaeHwang - 你是否按照這個答案改變了inner while循環和url的順序? – Sayse