如何将数据转换为csv文件

omvjsjqw  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(205)

我无法将使用bs4检索到的数据转换为有意义的csv文件。它只取实际检索到的最后一组数据。

#Beautiful soup or BS4 is  a package I will be using to allow me to parse the HTML data which I will be retrieving from a website.
#parsing is te conversion of codes from machine language into a code which humans can understand and allow it to be structured. 
#(Converting data from one format to another) with BS4
from bs4 import BeautifulSoup

#requests is an HTTP Library which allows me to send requests to websites the retrieve date using Python. This is helpful as 
#The website is writtin in a different language so it allows me to retrieve what I want and read it as well. 
import requests
#import writer

url= "https://myanimelist.net/anime/season"

#requesting to get data using 'requests' and gain acess as well. 
#hadve to check the response before moving forward to ensure there is no problem retrieving data. 

page= requests.get(url)
#print(page)
#<Response [200]> response was "200" meaning "Successful responses"


soup = BeautifulSoup(page.content, 'html.parser')
#here i retrieve my 
#for this to identify the html code and determine what we will be producing(retrieveing data) for each item on the page we had to 
#find the parent category which contains all the info we need to make our data categories. 
lists = soup.select("[data-genre]")
#we add _ after class  to make class_ because without the underscore the program identifies it as a python class 
# when really it is more of a cs class

    
all_data = []
#must create loop to find titles seperate as there are alot that will come up
for list in lists:
    #identify and find class which includes the title of the shows, show ratings, members watching, and episodes
    #added .text.replace in order to get rid of the|n spacing which was in html format
        title= list.find("a", class_="link-title").text.replace("\n", "")
        rating= list.find("div", class_="score").text.replace("\n", "")
        members= list.find("div", class_="scormem-item member").text.replace("\n", "")
        release_date= list.find("span", class_="item").text.replace("\n", "")
       
        all_data.append(
            [title.strip(), rating.strip(), members.strip(), release_date.strip()]
            )
        print(*all_data, sep="\n")
    #testing for errors and makins sure locations are correct to withdraw/request the data
#this allows us to create and close a csv file. using 'w' to allow editing

from csv import writer
#defining the website which I will be retrieving my code
#organizing chart
header=['Title', 'Show Rating', 'Members', 'Release Date']
info= [title.strip(), rating.strip(), members.strip(), release_date.strip()]

with open('shows.csv', 'w', encoding='utf8', newline='')as f:
   #will write onto our file 'f'
   writing = writer(f)
   #use our writer to write a row in file
   writing.writerow(header) 
    
   writing.writerow(info)

我试图改变列表的定义,但无济于事。这是目前我得到的。即使它应该长得多。

yhived7q

yhived7q1#

而不是只写最后一行

writing.writerow(info)

你需要写出所有的行:

writing.writerows(all_data)

相关问题