For Loop | Python

  • 本文关键字:Python Loop For python
  • 更新时间 :
  • 英文 :


我第一次使用Python进行网页抓取,在做代码时,我陷入了for循环。

如我们所见,b 和 a 都返回一个列表。我正在使用 for 循环来迭代 b 和 a 的列表值,并像序列一样打印它:

期望的输出

first value of b, first value of a, second value of b, second value of a....

这是代码:

b = soup.find("module", {"type":"Featured Link List Advanced"}).find_all("span")
a = soup.find("module", {"type":"Featured Link List Advanced"}).find("ul").find_all("a")
for i in b:
print (i.string)
for j in a:
print (j.get("href"))
break

但是我得到的输出是:

First value of b, First value of a, second value of b, first value of a, third value of, first value of a, 

有些人可以帮助我获得所需的输出吗?我知道我错过了什么。

使用内置 zip 并行迭代 2 个序列:

for (i,j) in zip(b,a):
print (i.string)
print (j.get("href"))

最新更新