[英]output .csv file only writing last query. How do I get it to print all url checks?
input.csv has a list of urls in one column to check. input.csv 有一列中的 url 列表要检查。 It runs these checks perfectly and then the script is supposed to write out which sites are up and which are down.
它完美地运行这些检查,然后脚本应该写出哪些站点启动,哪些站点关闭。 However, the last part of the code only seems to write the last site checked.
但是,代码的最后一部分似乎只写了最后一个站点检查。 How can I get it to write all checks to the output.csv?
如何让它将所有检查写入 output.csv?
import re
import requests
from itertools import product, cycle
import csv
from urllib.request import urlopen
with open("input.csv") as f:
for row in csv.reader(f):
s = 'Quantity' or 'Stock'
r = requests.get(row[0])
result = re.search(s, r.text)
if result == None:
print("OUT OF STOCK")
inis = "OUT OF STOCK"
else:
print("In Stock")
inis = "In Stock"
new_column = [(row[0]) + " " + inis]
print(row[0])
with open("output.csv", "w") as f:
for col in new_column:
f.write(col + "\n")
the script is supposed to write out which sites are up and which are down
该脚本应该写出哪些站点已启动,哪些站点已关闭
I'm not sure where the 'stock' terminology comes from, but assuming the logic code is correct, why not try something like this...我不确定“库存”术语的来源,但假设逻辑代码是正确的,为什么不尝试这样的事情......
Create an empty list first, then on each iteration of the loop append a row to it:首先创建一个空列表,然后在循环 append 的每次迭代中添加一行:
with open("input.csv") as f:
new_column = []
for row in csv.reader(f):
s = 'Quantity' or 'Stock'
r = requests.get(row[0])
result = re.search(s, r.text)
if result == None:
inis = "OUT OF STOCK"
else:
inis = "In Stock"
new_column.append(f"{row[0]},{inis}")
print(row[0], inis)
new_column
is now a list of CSV compatible rows, which can be written to output.csv
with your exisiting code. new_column
现在是 CSV 兼容行的列表,可以使用现有代码将其写入output.csv
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.