我的 `break` 在 while True 循环中不起作用
这是我Python脚本的一部分:
with open("/tmp/IpList.txt", "r+") as f:
for ip in f:
page = 1
ip = ip.strip("\r\n")
print ip
while True:
url = 'http://www.bing.com/search?q=ip%3a' + ip + '&qs=n&pq=ip%3a' + ip + '&sc=0-0&sp=-1&sk=&first=' + str(page) + '&FORM=PERE'
print url
with open("/tmp/content.txt", "a") as out:
content = ContentFunc()
if "<cite>" in content and "</cite>" in content:
out.write(content)
with open("/tmp/result.txt", "a") as result:
res = CiteParser()
result.write(res.encode('utf-8'))
page += 10
else:
break
当IpList.txt
里的IP地址用完后,它又会从第一个IP地址开始。为什么会这样呢?
谢谢...
2 个回答
0
我同意上面的说法,你可以使用:
with open("/tmp/IpList.txt", "r+") as f:
for ip in f:
page = 1
ip = ip.strip("\r\n")
print ip
loop_going = True # New var <---------------
while loop_going:
url = 'http://www.bing.com/search?q=ip%3a' + ip + '&qs=n&pq=ip%3a' + ip + '&sc=0-0&sp=-1&sk=&first=' + str(page) + '&FORM=PERE'
print url
with open("/tmp/content.txt", "a") as out:
content = ContentFunc()
if "<cite>" in content and "</cite>" in content:
out.write(content)
with open("/tmp/result.txt", "a") as result:
res = CiteParser()
result.write(res.encode('utf-8'))
page += 10
else:
loop_going = False # Set to false <---------------
1
你不需要使用while循环,把所有的IP地址放在一个列表里就可以了:
with open("a.txt", "r+") as f:
page = 1
ips = [ip.strip("\r\n") for ip in f]
for ip in ips:
url = 'http://www.bing.com/search?q=ip%3a' + ip + '&qs=n&pq=ip%3a' + ip + '&sc=0-0&sp=-1&sk=&first=' + str(page) + '&FORM=PERE'
print url
with open("/tmp/content.txt", "a") as out:
content = ContentFunc()
if "<cite>" in content and "</cite>" in content:
out.write(content)
with open("/tmp/result.txt", "a") as result:
res = CiteParser()
result.write(res.encode('utf-8'))
page += 10