Python爬虫语句:条件与循环探究
Python爬虫语句中,条件语句和循环语句是至关重要的组成部分。在Python爬虫入门教程中,我们将深入研究这些语句的使用。一、if语句示例
if x < 0:
x = 0
print("Negative changed to zero")
elif x == 0:
print("Zero")
elif x == 1:
print("Single")
else:
print("More")
if else三个关键词在同一个层次
elif部分有零个或多个,else部分是可选的
二、for语句
words = ["cat", "window", "defenestrate"]
for w in words:
print(w, len(w))
返回如下
cat 3
window 6
defenestrate 12
window 6
defenestrate 12
在迭代过程中修改迭代序列不安全,如果你想要修改你迭代的序列(例如,复制选择项),你可以迭代它的复本,使用切割标识就可以很方便的做到这一点:
words = ["cat", "window", "defenestrate"]
for w in words[:]:
# 在list的副本中循环
if len
下载地址
用户评论