[英]How to put two "while" loops together in this case?
我想设置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)
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
所以,我认为 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.