IronPython应用之WebAPI(Web测试,AutoPyTest自动化测试)

(一)辅助工具推荐

Python脚本编辑器:推荐CodeAssistor(AutoPyTest独立工具Plugs目录下)

(二)Python脚本例子

import clr, sys
clr.AddReference("SgmlReaderDll")
clr.AddReference("System.Xml")
clr.AddReference('IronPythonTest')

from IronPythonTest import *
from Sgml import *
from System.Net import *
from System.IO import TextReader,StreamReader
from System.Xml import *
from System.Text.UnicodeEncoding import UTF8

def fromHtml(textReader):
    sgmlReader = SgmlReader()
    sgmlReader.SystemLiteral = "C:\\Users\\NHN\\Documents\\Visual Studio 2010\\Projects\\IronPythonTest\\IronPythonTest\\bin\\Debug\\html.dtd"
    sgmlReader.WhitespaceHandling = WhitespaceHandling.All
    sgmlReader.CaseFolding = CaseFolding.ToLower
    sgmlReader.InputStream = textReader
   
    doc = XmlDocument()
    doc.PreserveWhitespace = True
    doc.XmlResolver = None
    doc.Load(sgmlReader)
    return doc
def getWebData(url, method, data = None, cookie = None, encoding = "UTF-8"):
    req = WebRequest.Create(url)
    req.Method = method
   
    if cookie != None:
        req.CookieContainer = cookie
   
    if data != None:
        stream = req.GetRequestStream()
        stream.Write(data, 0, data.Length)
       
    rsp = req.GetResponse()
    reader = StreamReader(rsp.GetResponseStream(), UTF8.GetEncoding(encoding))
    return reader

class Post:
    def __init__(self, hit, comments, title, link, author):
        self.hit = hit
        self.comments = comments
        self.title = title
        self.link = link
        self.author = author

class BaiDu:
    def __init__(self,encoding):
        self.cc = self.cc = CookieContainer()
        self.encoding = encoding
        self.posts = []
    def getPosts(self, url):
        reader = getWebData(url, "GET", None, self.cc, self.encoding)
        doc = fromHtml(reader)
        trs = doc.SelectNodes("html//table[@id='thread_list_table']/tbody/tr")
        self.parsePosts(trs)
    def parsePosts(self, trs):
        for tr in trs:           
            tds = tr.SelectNodes("td")
            hit = tds[0].InnerText
            comments = tds[1].InnerText
            title = tds[2].ChildNodes[1].InnerText
            link = tds[2].ChildNodes[1].Attributes["href"]
            author = tds[3].InnerText
           
            post = Post(hit, comments, title, link, author)
            self.posts.append(post)
           
baidu = BaiDu('GBK')
baidu.getPosts('http://tieba.baidu.com/f?kw=seo')
result = ''
for post in baidu.posts:
    result = result + post.title + ' (回复数:' + post.comments + ')(点击数:' + post.hit + ')[作者:' + post.author + ']\n'
#Ipy_this.txtScript.Text = result
#C#收到最后一行返回值
result + ''

(三)界面和效果

AutoPyTest获取:

资源下载:http://download.csdn.net/detail/w565911788/4323928(自解压绿色安装包)

需安装环境:dotNetFx40_Full_x86_x64.exe; IronPython-2.7.2.1.msi; (或以上)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值