python列表换行写入_python csv在写入的换行问题

此篇博客展示了如何使用Python通过requests和BeautifulSoup库从豆瓣电影Top250页面抓取电影名称、评分、推荐语和链接,然后将数据存储到CSV文件中。作者详细介绍了数据抓取过程和文件操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[Python] 纯文本查看 复制代码import requests

from bs4 import BeautifulSoup

import csv

movies_dict ={}

header = ['序号','电影名','评分','推荐语','URL']

for i in range(10):

res = requests.get('https://movie.douban.com/top250?start='+str(i*25)+'&filter=')

bs_res = BeautifulSoup(res.text,'html.parser')

num_all = bs_res.find_all('em',class_='')

title_all = bs_res.find_all('div',class_='hd')

star_all = bs_res.find_all('span',class_='rating_num')

tui_all = bs_res.find_all('span',class_='inq')

url_all = bs_res.find_all('div',class_='pic')

n =len(title_all)

a=0

for b in range(len(title_all)):

movies_list =[]

title = title_all[b].find(class_='title')

#print(num_all[a].text,title.text,star_all[a].text.replace('\n',''),tui_all[a].text,url_all[a].find('a')['href'])

movies_list = [num_all[a].text,title.text,star_all[a].text,tui_all[a].text,url_all[a].find('a')['href']]

a += 1

for e in range(5):

movies_dict[header[e]] = movies_list[e]

with open('movies1.csv','a+',newline='',encoding='utf-8') as movies_file:

movies_csv = csv.DictWriter(movies_file,fieldnames=header)

movies_file.writerow(movies_dict)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值