python:更改变量以在无限循环中重新运行脚本

2024-07-05 14:06:05 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上我试着用一个起始变量在我的代码中运行它。 然后给变量加1,然后重新运行代码并重复,直到我停止它。现在我已经写了这篇文章,但它离我要完成的目标还差得很远。我走的是正确的道路还是完全错了?你知道吗

代码-

import requests
from bs4 import BeautifulSoup as bs
import re

start = 1
print("starting number: ", start)
i = 0
number = 500
while i < number:
    url = "https://randomsite.com/{0}".format(start + i)
try:
    print(int(start) + i)

    response1 = requests.get(url)
    name = re.findall(('analyticsKey":"([^"]+)'), response1.text)
    ids = re.findall(('id":"([^"]+)'), response1.text)
    print(name)
    print("")
    print(ids)
    print("")
except:
    pass
i += 1

Tags: 代码textnameimportreidsurlnumber