我正在做一个网络抓取项目,我的代码正在工作。我只是不知道如何避免在cvs文件中的换行空格。Udemy讲师使用字典来存储所有数据,没有换行问题。这是避免这一切的唯一方法吗?
import requests
from bs4 import BeautifulSoup
from csv import writer
response = requests.get("http://quotes.toscrape.com/")
soup = BeautifulSoup(response.text, "html.parser")
quotes = soup.find_all(class_="quote")
with open("web_scraping.csv", "w") as f:
csv_writer = writer(f)
csv_writer.writerow(["Title", "Author", "Link"])
for quote in quotes:
texts = quote.find(class_="text").get_text()
author = quote.find(class_="author").get_text()
link = quote.find("a")["href"]
csv_writer.writerow([texts, author, link])
```
OUTPUT
```
Title,Author,Link
“The world as we have created it is a process of our thinking. It cannot be changed
without changing our thinking.”,Albert Einstein,/author/Albert-Einstein
"“It is our choices, Harry, that show what we truly are, far more than our
abilities.”",J.K. Rowling,/author/J-K-Rowling
“There are only two ways to live your life. One is as though nothing is a miracle. The
other is as though everything is a miracle.”,Albert Einstein,/author/Albert-Einstein
"“The person, be it gentleman or lady, who has not pleasure in a good novel, must be
intolerably stupid.”",Jane Austen,/author/Jane-Austen
"“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous
than absolutely boring.”",Marilyn Monroe,/author/Marilyn-Monroe
“Try not to become a man of success. Rather become a man of value.”,Albert
Einstein,/author/Albert-Einstein
“It is better to be hated for what you are than to be loved for what you are not.”,André
Gide,/author/Andre-Gide
"“I have not failed. I've just found 10,000 ways that won't work.”",Thomas A.
Edison,/author/Thomas-A-Edison
“A woman is like a tea bag; you never know how strong it is until it's in hot
water.”,Eleanor Roosevelt,/author/Eleanor-Roosevelt
"“A day without sunshine is like, you know, night.”",Steve Martin,/author/Steve-Martin
打开文件时使用attribute (newline= ")
with open(<filename>, mode='w', newline='') as file:
这将在写入csv文件时删除所有新的行字符。仍然字典是更好的和有条理的方式。