我正在使用beautifulsoup來取消某些圖像鏈接。採用這種編碼,我設法讓他們都將聲明打印到變量中
images = []
images = page_soup.findAll('img')
for image in images:
print(image.get('src'))
現在我想的鏈接寫入到一個csv文件,有沒有辦法把我的print語句到一個變量,這樣它會寫入一個行?
這裏是我到目前爲止的代碼
with open('index.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([name, images, datetime.now()])
更新我改變了我的代碼
images = []
images = page_soup.findAll('img')
with open('index.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
for image in images:
writer.writerow([image.get_text(), image.get('src'), datetime.now()])
但仍具有CSV格式問題,我想所有的圖像鏈接到同一行打印。
「這是我到目前爲止的代碼」 - 如果它不工作(這就是爲什麼你在這裏),而不是什麼發生? – hlt