Python bs4:如何重复;至于;如果满足某个条件,是否使用不同的表达式列表循环?

Python bs4:如何重复;至于;如果满足某个条件,是否使用不同的表达式列表循环?,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我试图创建一个for循环,一旦它到达第1页的最后一个注释索引属性,它将使用第2页的数据重复该循环 data\u page\u 1='' 第1页评论1 第1页评论2 ''' 数据\u页面\u 2=“” 第2页评论1 第2页评论2 ''' 从bs4导入BeautifulSoup soup=BeautifulSoup(数据第1页,'lxml') 查找所有(attrs={“注释索引”:True}): 打印(comment.text) 如果comment==soup.find_all(attrs={“co

我试图创建一个
for
循环,一旦它到达第1页的最后一个
注释索引
属性,它将使用第2页的数据重复该循环

data\u page\u 1=''
第1页评论1
第1页评论2
'''
数据\u页面\u 2=“”
第2页评论1
第2页评论2
'''
从bs4导入BeautifulSoup
soup=BeautifulSoup(数据第1页,'lxml')
查找所有(attrs={“注释索引”:True}):
打印(comment.text)
如果comment==soup.find_all(attrs={“comment index”:True})[-1]:
soup=BeautifulSoup(数据页面2,'lxml')
但是,上面的代码不会继续循环第2页的数据,尽管我为变量
soup
输入了一个新值,它只打印:

Page 1 Comment 1
Page 1 Comment 2
有人有什么见解吗 试着这样做:

data_pages = [data_page_1, data_page_2]

for page in data_pages:
    soup = BeautifulSoup(page, 'lxml')

    for comment in soup.find_all(attrs={"comment-index":True}):
        print(comment.text)
输出:

Page 1 Comment 1
Page 1 Comment 2
Page 2 Comment 1
Page 2 Comment 2