2017-01-27 1 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

whoa !!! 정말 고맙습니다. –

0

그래서, 나는 모두가 순서대로 증가한다 STR (장) 및 STR (책을) 생각 : 여기

내가 대한 질문이 나의 코드의 일부이다. 아니?

url이 새로운 챕터 번호로 업데이트되도록하려면 inner 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)로 실행하면된다. 그런 다음 처음 두 권의 책의 첫 번째 장을 가져올 수 있어야하지만 여전히 첫 번째 장의 장 1 장만 제공합니다. –

+0

@YunTaeHwang -이 답변에 따라 내부 while 루프와 url의 순서를 변경 했습니까? – Sayse

관련 문제