Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python - How do I write this data into a csv file

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Example of working code:

import requests
from bs4 import BeautifulSoup

## Get the html from the page at your url
request = requests.get('https://quotes.toscrape.com')

## Create a BS object from the request text
soup = BeautifulSoup(request.text, features="html.parser")

## Every block of text is a div with class="quote" so 
## lets start by pulling all of those
quotes = soup.find_all("div", {"class": "quote"})

## Before we itterate through all the quotes lets make a new csv
f = open("test.csv", "w")

## Lets write the headers into the csv
f.write("quote,author
")

## Now lets itterate through all of those quotes
for each in quotes:

    ## Now that we have each broken out you can find the individual tags you need
    quote = each.find("span", {"class": "text"}).text

    ## Each author is in a small tag so this one is easy
    author = each.find("small").text

    ## Now lets write each quote/author to the csv
    f.write(f'{quote},{author}
')

## Now we close the csv file
f.close()

I commented for clarity and understanding, let me know if you have any questions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...