hHw循环播放数字以交替填充列表

时间:2019-07-17 19:52:25

标签: python-3.x list loops

我有下面显示的代码,我正在尝试打印数字以填充列表。我在这里的值将索引到另一个列表。如何循环以使列表“(urls [i])”中i的值始终从零更改,直到达到length的值。

我已经尝试过了,但是它只有1-8的打印号码(这是名为url的列表的长度)

length=len(urls) //this is my input, the value of urls will 
            // depend on the result of my previeous code
i=0
while i<length:
  print(i)
  i += 1
result2 = requests.get(urls[i]) ///i want the value of i here  
        // to be increased by 1 everytime its looping
src2 = result2.content
soup = BeautifulSoup(src2, 'lxml')
links2 = soup.find_all('p')
print(links2)

1 个答案:

答案 0 :(得分:0)

我认为您只需缩进代码:

while i<length:
  print(i)
  i += 1
  //i want the value of i here to be increased by 1 everytime its looping
  result2 = requests.get(urls[i])
  src2 = result2.content
  soup = BeautifulSoup(src2, 'lxml')
  links2 = soup.find_all('p')
  print(links2)