I was doing a python exercise and I wanted put the author, tag, and text into a csv file, but I am having trouble figuring how to do so.
import requests
import bs4
import pandas as pd
import csv
res = requests.get('https://quotes.toscrape.com')
soup = bs4.BeautifulSoup (res.text,'lxml')
#soup
author = soup.select('.col-md-8')
example = author[1]
example.select('.text')
for item1 in example.select(".text"):
print(item1.text)
example.select('.tag')
for item2 in example.select(".tag"):
print(item2.text)
example.select ('div span')
for item3 in example.select(".author"):
print(item3.text)
file_to_output = open('QuotesToScrape.csv','w',newline='')
csv_writer = csv.writer(file_to_output,delimiter=',')
csv_writer.writerow(['Text','Tag','Author'])
csv_writer.writerows([[item3.text,item2.text,item1.text],['4','5','6']])
file_to_output.close()
question from:
https://stackoverflow.com/questions/66058270/how-do-i-write-this-data-into-a-csv-file 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…