本帖最后由 E飞翔 于 2018-11-21 21:26 编辑
知识点,XML的解析
[Python] 纯文本查看 复制代码 import requests
import xml.etree.ElementTree as ET
class STE_XML:
wrod = ''
begin = 0
max_num = 0
def __init__(self, data):
self.wrod = data
self.getPage()
def getPage(self):
""" 获取符合关键字的页码 """
datas = {
'selectColumnVal': '',
'word': self.wrod,
'begin': self.begin
}
res = requests.post("http://www.ste56.com.cn:8313/NewSearchPaiSongWeb/search.do", data=datas).text
root = ET.fromstring(res)
self.max_num = len(root)
return root
# for u in range(len(root)):
def getAllData(self):
""" 获取并且打印所有搜索到的数据 """
for u in range(self.max_num):
root = self.getPage()
if self.begin <= self.max_num:
self.begin += 1
for a in root:
for i in a:
print(i.text)
def getWordData(self):
""" 获取关键字为站点名的数据集合 """
for u in range(self.max_num):
root = self.getPage()
if self.begin <= self.max_num:
self.begin += 1
for a in root:
for i in a:
if i.text == self.wrod:
return a
def readWordData(self):
data = self.getWordData()
for i in data:
print(i.text)
if __name__ == '__main__':
p = STE_XML("虎门")
p.readWordData()
|