1. 首页
  2. 编程语言
  3. Python
  4. Python爬虫语句:条件与循环探究

Python爬虫语句:条件与循环探究

上传者: 2023-11-12 02:34:28上传 PDF文件 69.78KB 热度 23次

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

在迭代过程中修改迭代序列不安全,如果你想要修改你迭代的序列(例如,复制选择项),你可以迭代它的复本,使用切割标识就可以很方便的做到这一点:

words = ["cat", "window", "defenestrate"]

for w in words[:]:

# 在list的副本中循环

if len

下载地址
用户评论