(一)辅助工具推荐
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; (或以上)