我正在尝试逐行输出每一篇文章的“标题”和“日期”,我从一个特定区域从网页上划分到csv。
所以我知道:
with open("Output.csv", "a") as f:
wr = csv.writer(f)
...
wr.writerows([[title.text.strip()] for title in specific_area.findAll('h1')] + [[date.text.strip()] for date in specific_area.findAll('h6')])
这个输出首先为标题执行for循环,然后为之后的日期执行for循环,因此每个都在它们自己的行中。
如何让for循环同时完成?非常感谢!
尝试使用zip
函数。
wr.writerows([[title.text.strip(), date.text.strip()] for title, date in zip(specific_area.findAll('h1'), specific_area.findAll('h6'))])