Python 与数据存储
来源:互联网 发布:ubuntu 下载器推荐 编辑:程序博客网 时间:2024/06/11 12:34
1、存储媒体文件
媒体文件的存储有两种方式,一种是只存储媒体文件的url链接,这种方式对于媒体文件的url链接基本不发生变化,或对该媒体文件的引用只有一次的情况下较为适用;对于url链接经常变化,或引用次数较多的媒体文件,通常需要下载该每天文件到本地。
在Python3.x中,urllib.request.urlretrieve可以根据文件的url下载文件。下边的代码将实现从百度词条"英雄联盟"中下载正文中的图片。
from bs4 import BeautifulSoupimport reimport osfrom urllib.request import urlopenfrom urllib.request import urlretrievedownLoadDirectory="F:\\PythonScrawler\\LOL\\"url="http://baike.baidu.com/subview/3049782/11262116.htm"html=urlopen(url)bsObj=BeautifulSoup(html,"html.parser")def GetDownLoadPath(url,downLoadPath): pathSplit=url.split('/') length=len(pathSplit) if length>1: filename=pathSplit[length-1] filePathName=downLoadPath+filename directory=os.path.dirname(downLoadPath) if not os.path.exists(directory): os.makedirs(directory) return filePathNamefor node in bsObj.find("div",{"class":"main-content"}).findAll("div",{"class":"para"}): links=node.findAll("div",{"class":"lemma-picture text-pic layout-right"}) if len(links)>0: for link in links: children=link.findAll('img') for child in children: if child.has_attr('data-src'): if child.attrs['data-src']!=None: #print(child.attrs['data-src']) urlretrieve(child.attrs['data-src'],GetDownLoadPath(child.attrs['data-src'],downLoadDirectory))
在上边的代码中,GetDownLoadPath()函数用来获取要保存的文件的全名,主要代码注释如下;
os.path.dirname(downLoadPath) #获取文件路径
os.path.exists(directory)<span style="white-space:pre"></span>#判断路劲是否存在os.makedirs(directory)<span style="white-space:pre"></span>#生成路劲
在网页中,查看对应项的审查元素,发现图片的url位于html节点img中,链接在该节点的src属性中,但在python的BeautifulSoup中查看对应项,发现其属性为data-src中,具体原因待查,本文在代码中使用data-src属性。程序抓取的效果如下:
2、保存为CSV文件
CSV文件是存储表格数据的常用文件格式。下边的代码用于抓取百度词条php中的第一张表格,并保存在本地的CSV文件中。
#下载百度百科PHP词条中的表格#import csvfrom bs4 import BeautifulSoupfrom urllib.request import urlopenimport recsvFile=open('F:\\PythonScrawler\\PHPCSV\\set.csv','wt')writer=csv.writer(csvFile)url="http://baike.baidu.com/subview/99/5828265.htm"html=urlopen(url)bsObj=BeautifulSoup(html.read(),'html.parser')table=bsObj.findAll("table",{"class":"table-view log-set-param"})[0]rows=table.findAll("tr")#print(table)try: for row in rows: #print(row) csvRows=[] for col in row.findAll(re.compile("t(d|h)")): csvRows.append(col.get_text()) writer.writerow(csvRows)finally: csvFile.close()
在上边的代码中,首先打开了一个本地的CSV文件,并创建了该文件的writer对象,用于对该文件的写操作。在写的过程中,首先创建了一个空的序列,对于属于同一行不同列的数据,分别添加至同一序列中,然后用writer对象的writerow函数将该序列中的所有元素写如CSV文件的一行中。实验效果如下图:
3、保存至MySQL数据库中
使用Python对MySQL数据库进行操作,需要用到PyMySQL数据库,本文使用的版本为0.7.2。在刚刚装好的MySQL的数据库中,需要先对帐号进行访问授权,否则将出现数据库无法访问的错误。授权代码如下:
grant all privileges on *.* to root@'%' identified by '12345678' with grant option
上述代码中,允许帐号root,密码12345678在任意IP对数据库进行指定操作的访问。如果需要限定IP,可以使用:
grant all privileges on *.* to root@'xxx.xxx.xxx.xxx' identified by '12345678' with grant option;
其中,xxx.xxx.xxx.xxx为要指定的IP地址。
下边的代码为使用python抓取百度词条“英雄联盟”中的子词条,并将链接和子词条名保存在数据库中。
from bs4 import BeautifulSoupimport refrom urllib.request import urlopenimport pymysql## 百度百科:英雄联盟##html=urlopen("http://baike.baidu.com/subview/3049782/11262116.htm")bsObj=BeautifulSoup(html.read(),"html.parser")#print(bsObj.prettify())conn=pymysql.connect(host='202.115.52.234',unix_socket='/tmp/mysql.sock',user='Julian',passwd='Julian2016',db='mysql',charset='gbk')cur=conn.cursor()cur.execute("USE LOLset")#row=0for node in bsObj.find("div",{"class":"main-content"}).findAll("div",{"class":"para"}): links=node.findAll("a",href=re.compile("^(/view/)[0-9]+\.htm$")) for link in links: if 'href' in link.attrs: print(link.attrs['href'],link.get_text()) cur.execute("INSERT INTO pages (links,words) VALUES (\"%s\",\"%s\")",(link.attrs['href'],link.get_text())) cur.connection.commit() #row=row+1cur.close()conn.close()
由于词条名为中文,因此,在建立数据库连接的时候,需要指定字符集为gbk,
<pre name="code" class="python">conn=pymysql.connect(host='202.115.52.234',unix_socket='/tmp/mysql.sock',user='Julian',passwd='Julian2016',db='mysql',charset='gbk')
数据库名为LOLset,表名为pages。pages的定义如下:
实验的数据如下:
0 0
- Python 与数据存储
- Python中数据加载、存储与文件格式
- Python数据加载、存储与文件格式
- 利用Python进行数据分析--数据加载、存储与文件格式
- 利用Python数据分析:数据加载、存储与文件格式(一)
- python数据分析(数据检索、加工与存储)
- Python + Mongodb数据存储
- Python数据存储
- python数据持久存储
- Python 存储数据
- python/pandas/numpy(十二)数据加载、存储与文件格式
- 《Python数据分析与展示》学习笔记(一)numpy数据存储与函数
- 数据存储与传输
- 数据存储与ContentProvider
- #数据存储与访问
- 数据存储与检索
- 数据存储与访问
- 数据存储与访问
- 查找元素对应的事件(方法)
- jstl c:choose>、<c:when>和<c:otherwise>标签
- ROS_Kinetic_03 ROS入门向导
- asp.net 后台调用js
- ctf练习---摩斯密码
- Python 与数据存储
- IOS项目流水线开发步骤(初稿)
- UITextView 使用
- 关于开发产品的散谈
- 如何设置Android中TextView的行间距
- 在eclipse中如何更好的实现代码的code review
- 怎么用ChemBio 3D检查结构信息
- QByteArray QString Base64 UTF8
- apue.h头文件错误和error错误处理