简体   繁体   English

为什么我的 for 循环不能正常工作(Python)

[英]Why does my for loop doesn't work properly (Python)

So this my code所以这是我的代码

print("Search For The News")
AnimeName=input('>')
for i in range(1,10):
    url2='https://wall.alphacoders.com/search.php?search='
    url3=url2 + AnimeName.replace(" ","+")+(f"&page={i}")
    useragent=Request(url3, headers={'User-Agent':'Mozilla/5.0'})

    print(url3)

I get this same results 30 times https://wall.alphacoders.com/search.php?search=my+hero+academia&page=9我得到同样的结果 30 次https://wall.alphacoders.com/search.php?search=my+hero+academia&page=9

When I run this code it just repeats the same 9 instead of 2 to 9 why is it?当我运行此代码时,它只是重复相同的 9 而不是 2 到 9 为什么会这样? where am I going wrong here?我在哪里错了?

I was unable to reproduce your error, I used this code.我无法重现您的错误,我使用了此代码。

AnimeName='my hero academy'

url2='https://wall.alphacoders.com/search.php?search='

for i in range(1,10):
    url3=url2 + AnimeName.replace(" ","+")+(f"&page={i}")
#    useragent=Request(url3, headers={'User-Agent':'Mozilla/5.0'})
    print(url3)

output is output 是

https://wall.alphacoders.com/search.php?search=my+hero+academia&page=1
https://wall.alphacoders.com/search.php?search=my+hero+academia&page=2
https://wall.alphacoders.com/search.php?search=my+hero+academia&page=3
https://wall.alphacoders.com/search.php?search=my+hero+academia&page=4
https://wall.alphacoders.com/search.php?search=my+hero+academia&page=5
https://wall.alphacoders.com/search.php?search=my+hero+academia&page=6
https://wall.alphacoders.com/search.php?search=my+hero+academia&page=7
https://wall.alphacoders.com/search.php?search=my+hero+academia&page=8
https://wall.alphacoders.com/search.php?search=my+hero+academia&page=9

The most confusing part for me here is that you got the output 30 times, where you loop only over 10 values, pleases share the code that will reproduce that, and I'll try to help you further,对我来说最令人困惑的部分是您获得了 output 30 次,其中您仅循环了 10 个值,请分享将重现该代码的代码,我会尽力为您提供进一步的帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM