Browse Source

feat:添加Python数据处理文章

王智勇 4 years ago
parent
commit
27112487e5
2 changed files with 214 additions and 47 deletions
  1. 167 47
      package-lock.json
  2. 47 0
      source/_posts/python-data.md

File diff suppressed because it is too large
+ 167 - 47
package-lock.json


+ 47 - 0
source/_posts/python-data.md

@@ -0,0 +1,47 @@
+---
+title: python-data
+date: 2019-07-12 15:13:41
+tags: [python,reptile]
+categories: [python,reptile]
+---
+### Python 简单数据处理
+
+> 互联网中常见的数据基本基于 json xml 或者 html。本文介绍几种个人常用的组件,下载方式自行搜索,文中api仅供参考
+
+---
+
+#### json
+
+> Python自带json库,转换后和字典的操作方式相同
+
+常用api:
+
+- json.loads()   字符转json
+- json.dumps() json转字符
+
+#### xml
+
+> xml转换用的lxml组件 或 minidom组件
+
+lxml常用api:
+
+- xml_obj = etree.fromstring() 字符转xml
+- xml.find('') 查找节点
+- root = Element("xml")  创建xml root节点
+- sub = SubElement(root,'xx') 创建子节点
+- sub.text = '' 子节点赋值
+- etree.tostring(root, "UTF-8") xml转字符串
+
+**. minidom也可使用*
+
+#### html
+
+> html用的BeautifulSoup4库进项处理,html相对前两种来说相对复杂,我们需要提取到数据往往需要很多步骤,这种需要发现网页代码规律尽量找唯一性较高参数进行抓取。
+
+常用api:
+
+- html = BeautifulSoup(house_html, 'lxml') 创建html对象
+- html.find_all('li', attrs={'class': 'list-item', 'data-from': ''}) 查找标签 可自定属性和类别查询
+- .find(class_='house-title').a['title'] 单个查询节点下的信息
+
+使用中还可配合正则实现查询精准信息

Some files were not shown because too many files changed in this diff