亲爱的用户,全网VIP视频免费看模块已经打造完成,请点击菜单“免费看VIP”按钮进入免费看VIP视频页面!

使用python解析网页内容

Python专区 DannyWu 232℃ 已收录 0评论
 <pre class=”python” name=”code”>class GetWebResult():
def __init__(self):
jobs_id = 139366
self.jobs_url = ‘http://cbsp-wm-bl01/icase/api/rest/test/jobresults/?ids=%d’ % jobs_id@staticmethod

def get_soup(url):
headers = {‘User-Agent’:’Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6′}
req = urllib2.Request(url, headers=headers)
f = urllib2.urlopen(req, timeout=10)
html = f.read()
f.close()
return htmldef main(self):
save_path = os.path.join(os.getcwd(), ‘JobResult.xls’)
string_of_results = self.get_soup(self.jobs_url)
tmp = string_of_results.replace(‘null’, “\”null\””).replace(‘true’, “\”true\””).replace(‘false’, “\”false\””)
results = eval(tmp)[0].get(‘results’)
list_of_dict_result = []
for result in results:
list_of_dict_result.append(self.convert_result(result))
key_list = [‘Name’, ‘Image’, ‘Subsystem’, ‘Functionality’, ‘Objective’, ‘Assumptions’, ‘Steps’, ‘Tickets’, ‘Metrics’, ‘Notes’, ‘Result’]
xls = Excel(save_path)
xls.insert_sheet(sheet_name=’results’, list_dict=list_of_dict_result, list_key=key_list)

@staticmethod
def convert_result(dict):
dict_result = {}
case = dict.get(‘case’)
dict_result[‘Tickets’] = dict.get(‘tickets’)
dict_result[‘Metrics’] = dict.get(‘metrics’)
dict_result[‘Notes’] = dict.get(‘notes’)
dict_result[‘Result’] = dict.get(‘result’)
dict_result[‘Name’] = case.get(‘name’)
dict_result[‘Image’] = case.get(‘image’).get(‘name’)
dict_result[‘Subsystem’] = case.get(‘taxonomy’).get(‘subsystem’).get(‘name’)
dict_result[‘Functionality’] = case.get(‘taxonomy’).get(‘functional_area’).get(‘name’)
dict_result[‘Objective’] = case.get(‘objective’).decode(‘utf-8’)
dict_result[‘Assumptions’] = case.get(‘assumptions’)
dict_result[‘Steps’] = case.get(‘steps’).decode(‘utf-8’)
return dict_result</pre>

本站文章如未注明,均为原创丨本网站采用BY-NC-SA协议进行授权,转载请注明转自:http://idannywu.com/python5/.html
喜欢 (1)or分享 (0)

您必须 登录 才能发表评论!