Python脚本未在新行中写入结果-新手
我正在抓取IMDB页面中的数据,但当试图将其写入CSV文件时,我只从结果中获取最后一行 代码如下:Python脚本未在新行中写入结果-新手,python,html,css,csv,Python,Html,Css,Csv,我正在抓取IMDB页面中的数据,但当试图将其写入CSV文件时,我只从结果中获取最后一行 代码如下: from urllib.request import urlopen as uReq from bs4 import BeautifulSoup as soup my_url='http://www.imdb.com/search/title?genres=sci_fi&title_type=feature&sort=user_rating,desc' uClient = uReq
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url='http://www.imdb.com/search/title?genres=sci_fi&title_type=feature&sort=user_rating,desc'
uClient = uReq(my_url)
page_html=uClient.read()
uClient.close()
page_soup=soup(page_html,"html.parser")
images=page_soup.findAll("div",{"class":"lister-item-image float-left"})
containers=page_soup.findAll("div",{"class":"lister-item-content"})
filename="scifi.csv"
f=open(filename,"w")
headers="order, title, year"'\n'
f.write(headers)
for container in containers:
number=container.h3.findAll("span",{"class":"lister-item-index unbold text-primary"})
order=number[0].text
atitle=container.h3.findAll("a")
title=atitle[0].text
date=container.h3.findAll("span",{"class":"lister-item-year text-muted unbold"})
year=date[0].text
print("order:" + order)
print("title:" + title)
print("year:" + year)
f.write(order + "," +title+ "," +year + '\n')
f.close()
我用的是Anaconda&Spyder。看在youtube上,在google上搜索之后,出于对我的爱,我仍然无法理解为什么它没有一行一行地写。谢谢大家! 在for循环下添加f.write行。i、 e.添加缩进以匹配for循环体。您的f.write在for循环之外,因此它只写入最后一行。这应该起作用:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url='http://www.imdb.com/search/title?genres=sci_fi&title_type=feature&sort=user_rating,desc'
uClient = uReq(my_url)
page_html=uClient.read()
uClient.close()
page_soup=soup(page_html,"html.parser")
images=page_soup.findAll("div",{"class":"lister-item-image float-left"})
containers=page_soup.findAll("div",{"class":"lister-item-content"})
filename="scifi.csv"
f=open(filename,"w")
headers="order, title, year"'\n'
f.write(headers)
for container in containers:
number=container.h3.findAll("span",{"class":"lister-item-index unbold text-primary"})
order=number[0].text
atitle=container.h3.findAll("a")
title=atitle[0].text
date=container.h3.findAll("span",{"class":"lister-item-year text-muted unbold"})
year=date[0].text
print("order:" + order)
print("title:" + title)
print("year:" + year)
f.write(order + "," +title+ "," +year + '\n')
f.close()
嗯,您只有一个
f.write(…)
调用,直接在for循环体之外。所以,我怀疑这就是原因。请显示导出的输出以及代码当前输出的内容。使用调试器的结果是什么?我也很确定HTML和CSS标记是不相关的,因为你自己没有直接使用它们,问题不太可能出在这些部分上。此外,你真的应该使用csv
模块来创建csv。你是否尝试过使用中提到的csvwriter