您的位置:首页 > 编程语言 > Python开发

用Python下载Sharepoint数据列表

2016-08-08 11:24 155 查看

IQY Files to Make Web Query.

OQY file is a Microsoft Excel Web Query. In Microsoft Excel 97, Web queries allow you to query data from a specific World Wide Web, Internet, or intranet site and retrieve the information directly into a
Microsoft Excel worksheet.

In share point, it could export the data list through the query.iqy file. When open the file, it display as bellow, which is the link to download the remote list data.


WEB
1
https://sharepoint.xxx.com/qct/AP/marvell/APSS/APT/_vti_bin/owssvr.dll?XMLDATA=1&List={5CB2428F-228B-46CA-AA4E-48498F9BDE49}&View={29055B3C-1242-4E33-9ACB-DD3B4165165B}&RowLimit=0&RootFolder=%2fqct%2fAP%2fqctchinaeng%2fAPSS%2fAPT%2fLists%2fAPT%5fTask3

Download Web Contents in Python

Following are the modules to be imported for downloading. It is majorly based on Requests lib of Python, which is a simple HTTP library encapsulation.  import requests
from requests_ntlm import HttpNtlmAuth

Session object: allows to persists certain parameters across requests, following is an example to demonstrate how to get url with authentication information.

self.auth_NTLM = HttpNtlmAuth('user name','pass code')


def get_workload_data_from_web(self, url):
try:
session = requests.Session()
return session.get(url, auth=self.auth_NTLM).text
except Exception, e:
print e
return self.get_workload_data_from_web(url)
</pre><pre class="html" name="code"><span style="font-size:12px;">
</span>



Parse List Data as XML

import xml.dom.minidom as DOM
from xml.dom.minidom import Document

To use the built-in DOM object in python to parse the XML, following is a simple example.  

input_xml_string = """
<root>
<item>
<data version="1.0" url="http://***" />
<data version="2.0" url="http://***" />
</item>
<other>
<data version="1.0" url="http://***" />
<data version="2.0" url="http://***" />
</other>
</root>
"""

import xml.dom.minidom
def get_tagname():
doc = xml.dom.minidom.parseString(input_xml_string)
for node in doc.getElementsByTagName("data"):
print (node, node.tagName, node.getAttribute("version"))

Section 1 - Data table

<span style="font-size:12px;"><s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly' rs:CommandTimeout='30'>
<s:AttributeType <span style="color:#ff6666;">name</span>='ows_Week_x002d_end_x0020_Date' rs:name='Week-end Date' rs:number='1'>
<s:datatype dt:type='datetime' dt:maxLength='8' />
</s:AttributeType>
<s:AttributeType <span style="color:#ff6666;">name</span>='ows_Tester' rs:name='Tester' rs:number='2'>
<s:datatype dt:type='string' dt:lookup='true' dt:maxLength='512' />
</s:AttributeType></span>


Section 2 - Data contents

<span style="font-size:12px;"><rs:data>
<z:row ows_Week_x002d_end_x0020_Date='2016-08-06 00:00:00' ows_Tester='3117;#Wu, You' ows_Requester='6891;#Huang, Huang' ows_CE_x002f_CPL_x0020_work_x003f_='0' ows_Target='N/A' ows_PL='N/A' ows_Type_x0020_of_x0020_Work='Conference' ows_Sub_x002d_Area='N/A' ows_SW_x0020_Info_x002e_='NA' ows_Hardware_x0020_Type='N/A' ows_Hardware_x0020_Version='N/A' ows_Summary='<div class="ExternalClass815F44AAE4A343DB80D951152259A25F"><p>​weekly meeting<br></p></div>' ows_Efforts_x0020__x0028_in_x0020_Ho='2.00000000000000' ows_Team_x002d_Leader='Huang He' ows_Created='2016-08-05 17:54:43' ows_ID='660' /></span>

Use the dictionary to store get number by key.

def get_list_of_row_data_xls(self,xml_data,key_list):
list_row_data = []
rows = xml_data.getElementsByTagName('z:row')
for row in rows:
row_data = {}
for key in key_list:
row_data[key] = self.switch_handle_row_data_xls.get(key)(row.getAttribute(key))
list_row_data.append(row_data)
return list_row_data





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐