python数据保存为txt文件 | 您所在的位置:网站首页 › python将数组保存成txt › python数据保存为txt文件 |
#encoding:'utf-8' import urllib.request from bs4 import BeautifulSoup import os import time #找到网址 def getDatas(): url="https://movie.douban.com/top250" # url="file:///E:/scrapy/2018-04-27/movie/movie.html" # 打开网页 res=urllib.request.urlopen(url) # 转化格式 response=BeautifulSoup(res,'html.parser') # 找到想要数据的父元素 datas=response.find_all('div',{'class':'item'}) # print(datas) #创建存放数据的文件夹 folder_name="output" if not os.path.exists(folder_name): os.mkdir(folder_name) # 定义文件 current_time=time.strftime('%Y-%m-%d',time.localtime()) file_name="move"+current_time+".txt" # 文件路径 file_path=folder_name+"/"+file_name for item in datas: # print(item) rank=item.find('div',{'class':'pic'}).find('em').get_text() title=item.find('div',{'class':'info'}).find('div',{'class':'hd'}).find('a').find('span',{'class':'title'}).get_text() picUrl=item.find('div',{'class':'pic'}).find('a').find('img').get('src') # print(picUrl) # 保存数据为txt格式 try: with open(file_path,'a',encoding="utf-8") as fp: fp.write("排名:"+rank+'\n') fp.write("标题:"+title+'\n') fp.write("图片路径:"+picUrl+'\n\n') except IOError as err: print('error'+str(err)) finally: fp.close() pass getDatas() # 爬取数据 阅读更多 |
CopyRight 2018-2019 实验室设备网 版权所有 |