【问题标题】:How to put two "while" loops together in this case?在这种情况下如何将两个“while”循环放在一起?
【发布时间】:2017-01-27 17:14:24
【问题描述】:

我想设置max_chapter 并执行功能,直到章节号达到max_chapter。然后,书号加1,执行相同的函数,直到章节号达到max_chapter

例如,Book 1 - Chapter 1~20,Book 原来是 Book 2 并做函数 Book 2 - Chapter 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)

【问题讨论】:

  • 把它们放在一起是什么意思?
  • 哦...你知道我想从第 1 册的第 1~20 章中抓取所有文本,然后转到第 2 册的第 1 章并做同样的事情。所以,我认为 str(chapter) 和 str(book) 都应该按顺序增加。没有?

标签: python python-2.7 python-3.x beautifulsoup python-requests


【解决方案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

【讨论】:

    【解决方案2】:

    所以,我认为 str(chapter) 和 str(book) 都应该按顺序增加。没有?

    您只需在内部 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)
    

    【讨论】:

    • 注意:chapter &lt;= 1 仍然会限制您获得全部 20 章,但这是一个单独的问题
    • 是的,我可以更改为 20。我只是想检查它是否正常工作,因此,如果我将其设置为 1 并使用 start(2) 运行它。然后,它应该能给我前两本书的第一章,但它仍然只给我第一本书的第一章。
    • @YunTaeHwang - 您是否按照此答案更改了内部 while 循环和 url 的顺序?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2012-01-31
    • 2021-09-27
    相关资源
    最近更新 更多