溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用IronPython做網(wǎng)頁爬蟲

發(fā)布時間:2021-06-17 15:16:06 來源:億速云 閱讀:194 作者:chen 欄目:編程語言

本篇內(nèi)容介紹了“怎么用IronPython做網(wǎng)頁爬蟲”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

由于各種原因,我們經(jīng)常需要去別的網(wǎng)站采集一些信息,.net下所有相關(guān)的技術(shù)都已經(jīng)非常成熟,用Webrequest抓取頁面,既支持自定義Reference頭,又支持cookie,解析頁面一般都是用正則,而且對方網(wǎng)站結(jié)構(gòu)一變,還得重新改代碼,重新編譯,發(fā)布。

如果有了IronPython,可以把抓取和分析的邏輯做成Python腳本,如果對方頁面結(jié)構(gòu)變了,只需修改腳本就行了,不需重新編譯軟件,這樣可以用c#做交互和界面部分,用Python封裝預(yù)期經(jīng)常變化的部分。

安裝好IronPython和vs.net 2010后,還需要下載一個SGMLReader(見參考鏈接),這個組件可以把格式不是很嚴格的HTML轉(zhuǎn)換成格式良好的XML文件,甚至還能增加DTD的驗證

我們以抓取百度貼吧頁面為例,新建一個Console項目,引用IronPython,Microsoft.Dynamic,Microsoft.Scripting,SgmlReaderDll這些組件,把SGMLReader里的Html.dtd復(fù)制到項目目錄下,如果沒有這個,它會根據(jù)doctype去網(wǎng)絡(luò)上找dtd,然后新建baidu.py的文件,***在項目屬性的生成事件里寫上如下代碼,把這兩個文件拷貝到目標(biāo)目錄里

copy $(ProjectDir)\*.py $(TargetDir)  copy $(ProjectDir)\*.dtd $(TargetDir)

在baidu.py里首先引用必要的.net程序集

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

完了導(dǎo)入我們需要的類

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

利用SgmlReader寫一個把html轉(zhuǎn)換成xml的函數(shù),注意SystemLiteral屬性必須設(shè)置,否則就會去網(wǎng)上找dtd了,浪費時間

def fromHtml(textReader):      sgmlReader = SgmlReader()      sgmlReader.SystemLiteral = "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

利用webrequest寫一個支持cookie和網(wǎng)頁編碼的抓網(wǎng)頁方法

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

寫一個類來定義抓取結(jié)果,這個類不需要在c#項目里定義,到時候直接用c# 4.0的dynamic關(guān)鍵字就可以使用

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

定義主要工作的類,__init__大概相當(dāng)于構(gòu)造函數(shù),我們傳入編碼參數(shù),并初始化cookie容器和解析結(jié)果,[]是python里的列表,大約相當(dāng)于c#的List

class BaiDu:      def __init__(self,encoding):          self.cc = self.cc = CookieContainer()                  self.encoding = encoding          self.posts = []

接下來定義抓取方法,調(diào)用getWebData抓網(wǎng)頁,然后用fromHtml轉(zhuǎn)換成xml,剩下的就是xml操作,和.net里一樣,一看便知

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)

c#代碼要創(chuàng)建一個腳本運行環(huán)境,設(shè)置允許調(diào)試,然后執(zhí)行baidu.py,***創(chuàng)建一個Baidu的類的實例,并用dynamic關(guān)鍵字引用這個實例

Dictionary options = new Dictionary();  options["Debug"] = true;  ScriptEngine engine = Python.CreateEngine(options);  ScriptScope scope = engine.ExecuteFile("baidu.py");  dynamic baidu = engine.Operations.Invoke(scope.GetVariable("BaiDu"), "GBK");

接下來調(diào)用BaiDu這個python類的方法獲取網(wǎng)頁抓取結(jié)果,然后輸出就可以了

baidu.getPosts("http://tieba.baidu.com/f?kw=seo");              dynamic posts = baidu.posts;              foreach (dynamic post in posts)              {                  Console.WriteLine("{0}   (回復(fù)數(shù):{1})(點擊數(shù):{2})[作者:{3}]",                      post.title,                      post.comments,                      post.hit,                      post.author);              }

“怎么用IronPython做網(wǎng)頁爬蟲”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI