Python 如果变量中的值增加了一个特定的数字,请执行以下操作:

Python 如果变量中的值增加了一个特定的数字,请执行以下操作:,python,python-3.x,Python,Python 3.x,也许我想得不对: 当我刮一个网站,我得到3个值。我想将这些值打印到excel,它在下面的3个单元格中打印3个值,然后更改行,打印3个值更改行,依此类推 以下是我目前得到的信息: from bs4 import BeautifulSoup import requests import openpyxl r = requests.get("https://www.hemnet.se/bostader? location_ids%5B%5D=18045&item_types%5B%5D

也许我想得不对:

当我刮一个网站,我得到3个值。我想将这些值打印到excel,它在下面的3个单元格中打印3个值,然后更改行,打印3个值更改行,依此类推

以下是我目前得到的信息:

from bs4 import BeautifulSoup
import requests
import openpyxl



r = requests.get("https://www.hemnet.se/bostader? 
location_ids%5B%5D=18045&item_types%5B%5D=bostadsratt")



soup = BeautifulSoup(r.text, "html.parser")



rad = 2
kol = 1


for pris_kvd_rum in soup.find_all("div", class_="listing-card__attribute 
listing-card__attribute--primary"):


  pris = pris_kvd_rum.text


  wb = openpyxl.load_workbook("hemnet.xlsx")
  ws = wb.active

  ws.cell(row = rad, column = kol).value = pris


  kol += 1

  if kol % 4 == 0:
    rad +=1



wb.save("hemnet.xlsx")

如果您想每第四次迭代做一件事,您可以使用:

if kol % 4 == 0:
...

如果kol==kol+4:从来都不是
True

kol==kol+4
就永远不会是True,而不是
。整数不能等于自身加4。你打算做什么?我已经编辑了这个问题